Swift 파일 업로드, 비디오 크기 가져오기, 비디오 크기 가져오기 및 이미지 크기 가져오기 요약



머리말

지난 이틀 동안 Swift 프로젝트를 유지보수하면서 몇 가지 문제에 직면했습니다.


1. 영상에 대하여

1. 동영상 크기를 확인하고 동영상 파일 URL을 전달합니다.

 static func getVideoSize(by url: URL?) -> CGSize {
    
    
        var size: CGSize = .zero
        guard let url = url else {
    
    
            return size
        }
        let asset = AVAsset(url: url)
        let tracks = asset.tracks(withMediaType: AVMediaType.video)
        guard let track = tracks.first else {
    
    
            return size
        }

        let t = track.preferredTransform
        size = CGSize(width: track.naturalSize.height, height: track.naturalSize.width)

        if t.a == 0 && t.b == 1.0 && t.c == -1.0 && t.d == 0 {
    
    
            size = CGSize(width: track.naturalSize.height, height: track.naturalSize.width)
        } else if t.a == 0 && t.b == -1.0 && t.c == 1.0 && t.d == 0 {
    
    
            // PortraitUpsideDown
            size = CGSize(width: track.naturalSize.height, height: track.naturalSize.width)
        } else if t.a == 1.0 && t.b == 0 && t.c == 0 && t.d == 1.0 {
    
    
            // LandscapeRight
            size = CGSize(width: track.naturalSize.width, height: track.naturalSize.height)
        }else if t.a == -1.0 && t.b == 0 && t.c == 0 && t.d == -1.0 {
    
    
            // LandscapeLeft
            size = CGSize(width: track.naturalSize.width, height: track.naturalSize.height)
        }
        return size
    }

2. 비디오 파일 크기 가져오기

func wm_getFileSize(_ url:URL) -> Double {
    
    
        if let fileData:Data = try? Data.init(contentsOf: url) {
    
    
            let size = Double(fileData.count) / (1024.00 * 1024.00)
            return size
        }
        return 0.00
    }

3. 비디오 길이를 얻으려면 실제로 Asset.duration을 직접 사용할 수 있습니다. 자산은 PHAsset 유형입니다.

        let asset11 = AVURLAsset(url: videoURL) as AVURLAsset
        let totalSeconds = Int(CMTimeGetSeconds(asset11.duration))
        let minutes = totalSeconds / 60
        let seconds = totalSeconds % 60
        let mediaTime = String(format:"%02i:%02i",minutes, seconds)
        print("打印视频的时长=======",totalSeconds)

4. 비디오 URL 얻기

let videoFileName = "\(asset.localIdentifier).mp4".replace("-", new: "").replace("/", new: "")
let videoURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(videoFileName)

5. 비디오의 첫 번째 프레임 가져오기

    static func splitVideoFileUrlFps(splitFileUrl: URL, fps: Float, splitCompleteClosure: @escaping (Bool, [UIImage]) -> Void) {
    
    

        var splitImages = [UIImage]()
        let optDict = NSDictionary(object: NSNumber(value: false), forKey: AVURLAssetPreferPreciseDurationAndTimingKey as NSCopying)
        let urlAsset = AVURLAsset(url: splitFileUrl, options: optDict as? [String: Any])

        let cmTime = urlAsset.duration
        let durationSeconds: Float64 = CMTimeGetSeconds(cmTime) //视频总秒数

        var times = [NSValue]()
        let totalFrames: Float64 = durationSeconds * Float64(fps) //获取视频的总帧数
        var timeFrame: CMTime

        for i in 0...Int(totalFrames) {
    
    
            timeFrame = CMTimeMake(value: Int64(i), timescale: Int32(fps)) //第i帧, 帧率
            let timeValue = NSValue(time: timeFrame)

            times.append(timeValue)
        }

        let imgGenerator = AVAssetImageGenerator(asset: urlAsset)
        imgGenerator.requestedTimeToleranceBefore = CMTime.zero //防止时间出现偏差
        imgGenerator.requestedTimeToleranceAfter = CMTime.zero
        imgGenerator.appliesPreferredTrackTransform = true //不知道是什么属性,不写true视频帧图方向不对

        let timesCount = times.count

        //获取每一帧的图片
        imgGenerator.generateCGImagesAsynchronously(forTimes: times) {
    
     (requestedTime, image, actualTime, result, error) in

            //times有多少次body就循环多少次。。。

            var isSuccess = false
            switch (result) {
    
    
            case AVAssetImageGenerator.Result.cancelled:
                print("cancelled------")

            case AVAssetImageGenerator.Result.failed:
                print("failed++++++")

            case AVAssetImageGenerator.Result.succeeded:
                let framImg = UIImage(cgImage: image!)
                splitImages.append(framImg)

                if (Int(requestedTime.value) == (timesCount - 1)) {
    
     //最后一帧时 回调赋值
                    isSuccess = true
                    splitCompleteClosure(isSuccess, splitImages)
                    print("completed")
                }
            }
        }
    }

2. 사진 정보

1. 이미지 크기 가져오기

static func getImageSize(asset:PHAsset) -> Float{
    
    
        let resource = PHAssetResource.assetResources(for: asset).first
        if let size = resource?.value(forKey: "fileSize") as? Float {
    
    
            return size/(1024*1024)
        }
        return 0.0
    }

2. 온라인 이미지의 가로 및 세로 화면을 결정합니다.

    static func isLandscapeImage(_ imageUrl: String?) -> Bool {
    
    
        guard let imageUrl = imageUrl else {
    
    
            return true
        }
        let urlQuery = imageUrl.components(separatedBy: ",")
        //获取长宽值
        guard urlQuery.count > 2,
              let widthStr = urlQuery.first(where: {
    
    $0.hasPrefix("w_")}),
              let heightStr = urlQuery.first(where: {
    
    $0.hasPrefix("h_")}) else {
    
    
            return true
        }

        //从字符串中获取Int类型的长宽
        let nonDigits = CharacterSet.decimalDigits.inverted
        guard let width = Int(widthStr.trimmingCharacters(in: nonDigits)),
              let height = Int(heightStr.trimmingCharacters(in: nonDigits)) else {
    
    
            return true
        }

        if width > height {
    
    
            return true
        }
        return false
    }

3. 사진의 중앙을 중앙으로 사용하고 가장 작은 변을 변의 길이로 사용하여 정사각형 사진을 자릅니다.

    static func cropSquareImage(image: UIImage?) -> UIImage? {
    
    
        //将UIImage转换成CGImageRef
        guard  let image = image,
               let sourceImageRef = image.cgImage else {
    
    
            return nil
        }

        let imageWidth = image.size.width * image.scale
        let imageHeight = image.size.height * image.scale
        let width = imageWidth > imageHeight ? imageHeight : imageWidth
        let offsetX = (imageWidth - width) / 2
        let offsetY = (imageHeight - width) / 2

        let rect = CGRect(x: offsetX, y: offsetY, width: width, height: width)
        var newImage: UIImage? = nil
        if let aRef = sourceImageRef.cropping(to: rect) {
    
    
            //按照给定的矩形区域进行剪裁
            newImage = UIImage(cgImage: aRef)
        }

        return newImage
    }

3. 이미지 업로드에 대하여

1. Alamofire를 사용하여 파일을 넣고 업로드합니다.

        let sUrl = URL.init(string: putUrl)

        if sUrl == nil {
    
     return }

        let encoder = JSONEncoder()
        let jsonData = try! encoder.encode(data)

        let data1 = "last_value=no".data(using: String.Encoding(rawValue: NSUTF8StringEncoding))

        var request = URLRequest(url: sUrl!)
        request.httpMethod = "PUT"
        request.setValue("image/*", forHTTPHeaderField: "Content-Type")
        request.timeoutInterval = 1000
        request.httpBody = data
        print("data=====",jsonData,"httpBody====",request.httpBody,"httpMethod====",request.httpMethod,"allHTTPHeaderFields====",request.allHTTPHeaderFields,"request====",request)

            Alamofire.request(request).responseJSON {
    
     response in

                print("打印上传====",response)
                switch response.result {
    
    
                case .success(let value):
                    print ("finish")
                    let swiftyJson = JSON(value)
                    print("打印上传====",swiftyJson)
//                    completionHandler(swiftyJson, nil)
                case .failure(let error):
                    print("打印上传错误====",error)
//                    completionHandler(nil, error)
                }
            }

2. Alamofire의 기본 업로드 및 반환 결과에 대한 데이터 분석

/**
 TODO: 二进制流的形式上传
 @data : 上传文件的数据对象
 @URLConvertible : 上传的地址
 
 注意: data的类型是Data,不能直接使用 NSData类型的对象。否则会出现 “Cannot invoke 'upload' with an argument list of type '(NSData?, to: String)'”错误提示。
 */
let dataPath = Bundle.main.path(forResource: "upDataName", ofType: "upDataSuffix")
let upData = NSData.init(contentsOfFile: dataPath!)
let upLoadRequest:UploadRequest = Alamofire.upload(Data.init(referencing: upData!), to: "https://network.net/upload")
// 上面的请求返回一个 “UploadRequest” 对象。我们可以对其进行解析,Alamofire请求数据解析的方法都使用与它。
upLoadRequest.validate().response {
    
     (DDataRequest) in
    if let acceptData = DDataRequest.data {
    
    
        print(String.init(data: acceptData, encoding: String.Encoding.utf8)!)
    }
    if DDataRequest.error != nil {
    
    
        print("上传失败!!!")
    }
}

upLoadRequest.validate().responseJSON {
    
     (DataResponse) in
    if DataResponse.result.isSuccess {
    
    
        print(String.init(data: DataResponse.data!, encoding: String.Encoding.utf8)!)
    }
    if DataResponse.result.isFailure {
    
    
        print("上传失败!!!")
    }
}

upLoadRequest.validate().responseString {
    
     (DataResponse) in
    if DataResponse.result.isSuccess {
    
    
        print(String.init(data: DataResponse.data!, encoding: String.Encoding.utf8)!)
    }
    if DataResponse.result.isFailure {
    
    
        print("上传失败!!!")
    }
}

upLoadRequest.validate().responsePropertyList {
    
     (DataResponse) in
    if DataResponse.result.isSuccess {
    
    
        print(String.init(data: DataResponse.data!, encoding: String.Encoding.utf8)!)
    }
    if DataResponse.result.isFailure {
    
    
        print("上传失败!!!")
    }
}

upLoadRequest.validate().response(queue: DispatchQueue.main) {
    
     (DDataRequest) in
    if let acceptData = DDataRequest.data {
    
    
        print(String.init(data: acceptData, encoding: String.Encoding.utf8)!)
    }
    if DDataRequest.error != nil {
    
    
        print("上传失败!!!")
    }
}

3. Alamofire 파일 업로드 진행

/**
 TODO: 二进制流的形式上传
 @data : 上传文件的数据对象
 @URLConvertible : 上传的地址
 
 注意: data的类型是Data,不能直接使用 NSData类型的对象。否则会出现 “Cannot invoke 'upload' with an argument list of type '(NSData?, to: String)'”错误提示。
 */
let dataPath = Bundle.main.path(forResource: "upDataName", ofType: "upDataSuffix")
let upData = NSData.init(contentsOfFile: dataPath!)
let upLoadRequest:UploadRequest = Alamofire.upload(Data.init(referencing: upData!), to: "https://network.net/upload")
/**
 可获取上传进度
 */
upLoadRequest.uploadProgress {
    
     (progress) in
     // 上传进度
     print(progress.fractionCompleted)
}

upLoadRequest.uploadProgress(queue: DispatchQueue.main) {
    
     (progress) in
    // 上传进度
    print(progress.fractionCompleted)
}

4. Alamofire 데이터 유형 업로드

/**
Data 数据流
File 文件路径
stream 输入流
multipartFormData 多文件上传

 TODO: 二进制流的形式上传
 @data : 上传文件的数据对象
 @URLConvertible : 上传的地址
 
 注意: data的类型是Data,不能直接使用 NSData类型的对象。否则会出现 “Cannot invoke 'upload' with an argument list of type '(NSData?, to: String)'”错误提示。
 */
let dataPath = Bundle.main.path(forResource: "upDataName", ofType: "upDataSuffix")
let upData = NSData.init(contentsOfFile: dataPath!)
let upLoadRequest:UploadRequest = Alamofire.upload(Data.init(referencing: upData!), to: "https://network.net/upload")
// 上面的请求返回一个 “UploadRequest” 对象。我们可以对其进行解析,Alamofire请求数据解析的方法都使用与它。
upLoadRequest.validate().response {
    
     (DDataRequest) in
    if let acceptData = DDataRequest.data {
    
    
        print(String.init(data: acceptData, encoding: String.Encoding.utf8)!)
    }
    if DDataRequest.error != nil {
    
    
        print("上传失败!!!")
    }
}

5. Alamofire 파일 형식 업로드

/**
 TODO: 通过文件路径上传
 
 @fileURL : 上传文件的路径
 @URLConvertible : 上传文件的网址
 
 注意: fileURL 要转化一下,否则会报 “Cannot invoke 'upload' with an argument list of type '(String, to: String)'”错。
 */
Alamofire.upload(URL.init(fileURLWithPath: dataPath!), to: "https://network.net/upload").validate().responseData {
    
     (DDataRequest) in
    if DDataRequest.result.isSuccess {
    
    
        print(String.init(data: DDataRequest.data!, encoding: String.Encoding.utf8)!)
    }
    if DDataRequest.result.isFailure {
    
    
        print("上传失败!!!")
    }
}

6. Alamofire의 multipartFormData 양식 업로드

/**
 TODO: 多部分数据上传
 */
Alamofire.upload(multipartFormData: {
    
     (MFData) in
    MFData.append(upData! as Data, withName: "NetWork")
    MFData.append(upData! as Data, withName: "NetWork小贱")
}, to: "https://network.net/upload") {
    
     (SMResult) in
    switch SMResult {
    
    
    case .success(let request, let streamingFromDisk, let streamFileURL):
        print(streamFileURL!,request,streamingFromDisk)
        request.responseJSON(completionHandler: {
    
     (DResponse) in
            if DResponse.result.isSuccess {
    
    
                print("上传成功!!!")
            }
        })
        break
    case .failure(_):
        print("上传失败!!!")
        break
    }
}

7. 데이터 유형 요청 업로드

/**
 TODO: 可以自定义请求的二进制流上传
 */
let uploadRequest = URLRequest.init(url: URL.init(fileURLWithPath: dataPath!), cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 20)
Alamofire.upload(upData! as Data , with:uploadRequest).validate().responseJSON {
    
     (dataResponse) in
    if dataResponse.result.isSuccess {
    
    
        let acceptData = dataResponse.data
        print(String.init(data: acceptData!, encoding: String.Encoding.utf8)!)
    }
    if dataResponse.result.isFailure {
    
    
        print("上传失败!!!")
    }
}

8. 파일 형식 요청 업로드

/**
 TODO: 可以自定义请求的数据路径形式上传
 */
Alamofire.upload(URL.init(fileURLWithPath: dataPath!), with: uploadRequest).validate().response(queue: DispatchQueue.main) {
    
     (DDResponse) in
    if let acceptData = DDResponse.data {
    
    
        print(String.init(data: acceptData, encoding: String.Encoding.utf8)!)
    }
    if DDResponse.error != nil {
    
    
        print("上传失败!!!")
    }
}

9. 스트림 유형 업로드

/**
 TODO: 可以自定义请求的数据输入流形式上传
 */
Alamofire.upload(inPutStraeam, with: uploadRequest).validate().responseJSON {
    
     (DataResponse) in
    if DataResponse.result.isSuccess {
    
    
        print("上传成功")
    }
    if DataResponse.result.isFailure {
    
    
        print("上传失败!!!")
    }
}

10. MultipartFormData 유형 업로드

/**
 TODO: 可定义请去形式的多部分上传
 */
Alamofire.upload(multipartFormData: {
    
     (MFData) in
    MFData.append(upData! as Data, withName: "NetWork")
    MFData.append(upData! as Data, withName: "NetWork小贱")
}, with: uploadRequest) {
    
     (SMResult) in
    switch SMResult {
    
    
    case .success(let request, let streamingFromDisk, let streamFileURL):
        print(streamFileURL!,request,streamingFromDisk)
        request.responseJSON(completionHandler: {
    
     (DResponse) in
            if DResponse.result.isSuccess {
    
    
                print("上传成功!!!")
            }
        })
        break
    case .failure(_):
        print("上传失败!!!")
        break
    }
}

11、

요약하다

지난 이틀 동안 겪었던 문제와 방법을 요약해 보았는데, 도움이 되셨으면 좋겠습니다!

추천

출처blog.csdn.net/smileKH/article/details/130623677