gpt4 book ai didi

ios - 如何在后台上传 PHAssets

转载 作者:行者123 更新时间:2023-11-29 11:49:37 26 4
gpt4 key购买 nike

在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com