ios - 如何在后台上传 PHAssets

标签 ios objective-c afnetworking

在 Objective-C 中,我想在后台将所有 PHAssets(图像和视频)上传到我的服务器。任何人都可以建议我该怎么做吗?

最佳答案

let mPhasset : PHAsset = PHAsset()
if mPhasset.mediaType == .image {
    let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
    options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
        return true
    }
    mPhasset.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable: Any]) -> Void in
        if let url = contentEditingInput?.fullSizeImageURL {
            // IMPORTANT
            // this url avaiable only in this block
            // we can't perform async operation in bg
            // cause access will expired

            // Now you can copy using FileManager
            // and after this upload copied file
        }
    })

    // Alternative

    let imageManager = PHCachingImageManager()
    let opt = PHImageRequestOptions()
    opt.resizeMode = .exact
    opt.deliveryMode = .highQualityFormat

    imageManager.requestImage(for: mPhasset,
                              targetSize: CGSize(width: mPhasset.pixelWidth, height: mPhasset.pixelHeight),
                              contentMode: .aspectFill,
                              options: opt,
                              resultHandler:
        { (image: UIImage?, _) in
        // now you can go to background and do whatever you want
        // benefit that you don't need to copy file in main thread
        // which can lag your app
        // disadvantage of this method: we load image in memory
    })

} else if mPhasset.mediaType == .video {
    let options: PHVideoRequestOptions = PHVideoRequestOptions()
    options.version = .current

    PHImageManager.default().requestExportSession(forVideo: mPhasset, options: options, exportPreset: AVAssetExportPresetHighestQuality, resultHandler: { (session, info) in
        // if we just request video url asset and try upload to server
        // we will have problems with slow mo videos
        // also we will be on the main thread and access for video will expire, if we go to background
        // + size of video is very huge

        guard let session = session else { return }
        session.outputURL = URL(fileURLWithPath: "our output path")
        session.outputFileType = AVFileTypeQuickTimeMovie
        session.shouldOptimizeForNetworkUse = true
        session.exportAsynchronously {
            if (session.status == .completed) {
                // success
            } else {
            }
        }
    })
}

关于ios - 如何在后台上传 PHAssets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41696861/

相关文章:

ios - 没有捐赠的 Siri Intents 在设置中不可见

Objective-C:如何避免意外调用具有相同名称的私有(private)父类(super class)方法?

ios - 使用 AFNetworking 2 进行 POST 请求时 cocoa 错误 3840

ios - AFNetworking POST 写入服务器并返回失败错误(Cocoa 错误 3840)

ios - 使用 AFNetworking 登录 Instapaper

ios - 企业帐户配置文件过期后应用程序将崩溃

iphone - 如果定义了 didFinishPickingMediaWithInfo,相机会卡住

ios - 如果我们对同一个 IBOutlet 同时使用 strong 和 retain 会怎样?

iphone - 如何将所选 ID 与数组中的国家/地区 ID 进行比较并显示其名称而不是 ID

iphone - 如何转换字符串-> pdf