我想要对来自图库的图像执行编辑,例如,JEPG压缩(由iPhone摄像机拍摄),但是当输入图像是HEIC图像但与JPEG图像一起工作时,它会失败。
我通过PHAsset方法检索UIImagePickerController对象中的图像:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
self.asset = asset
}
//...
} 此函数编辑选定的图像:
func editImage() {
if let _asset = self.asset {
_asset.requestContentEditingInput(with: nil, completionHandler: { (contentEditingInput, info) in
let fullURL: URL?
fullURL = contentEditingInput!.fullSizeImageURL
let output = PHContentEditingOutput(contentEditingInput:
contentEditingInput!)
let archivedData = try? NSKeyedArchiver.archivedData(withRootObject: "HEICEditor", requiringSecureCoding: false)
let adjustmentData =
PHAdjustmentData(formatIdentifier:
"HEICEditor.App",
formatVersion: "1.0",
data: archivedData!)
output.adjustmentData = adjustmentData
let imageData = UIImage.init(contentsOfFile: fullURL!.path)?.jpegData(compressionQuality: 0.5)
do {
try imageData!.write(to: output.renderedContentURL, options: .atomic)
} catch let error {
print("error writing data:\(error)")
}
PHPhotoLibrary.shared().performChanges({
let request = PHAssetChangeRequest(for: _asset)
request.contentEditingOutput = output
}, completionHandler: { (result, error) in
print("error writing data:\(error)")
})
})
}
}带有示例HEIC映像的项目可在https://github.com/maysamsh/Swift-Playground-EditHEIC上使用。
发布于 2019-08-02 09:39:56
如下所示,如下所示:创建一个CGImageDestination,在.renderedContentURL上编写输出,执行the PHAssetChangeRequest()
func editImage() {
if let _asset = self.asset {
let options = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData)
-> Bool in
return true
}
_asset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
let fullURL: URL?
fullURL = contentEditingInput!.fullSizeImageURL
let output = PHContentEditingOutput(contentEditingInput:
contentEditingInput!)
let archivedData = try? NSKeyedArchiver.archivedData(withRootObject: "HEICEditor", requiringSecureCoding: false)
let adjustmentData =
PHAdjustmentData(formatIdentifier:
"HEICEditor.App",
formatVersion: "1.0",
data: archivedData!)
let orientation = contentEditingInput?.fullSizeImageOrientation
let outputURL = output.renderedContentURL
let cgImage = {
() -> CGImage in
let image = UIImage.init(contentsOfFile: fullURL!.path)!
let imageData = image.jpegData(compressionQuality: 1)
let ciImage = CIImage(data: imageData!)!.oriented(forExifOrientation: orientation!)
return CIContext(options: nil).createCGImage(ciImage, from: ciImage.extent)!
}()
let cgImageDestination = CGImageDestinationCreateWithURL(outputURL as CFURL, kUTTypeJPEG, 1, nil)!
CGImageDestinationAddImage(cgImageDestination, cgImage, [
kCGImageDestinationLossyCompressionQuality as String:0.7
] as CFDictionary)
CGImageDestinationFinalize(cgImageDestination)
output.adjustmentData = adjustmentData
self.infoLabel.text = "fullSizeImageURL: \(fullURL?.lastPathComponent ?? "N/A")\n" +
"renderedContentURL: \(output.renderedContentURL.lastPathComponent)"
PHPhotoLibrary.shared().performChanges({
let request = PHAssetChangeRequest(for: _asset)
request.contentEditingOutput = output
}, completionHandler: { (result, error) in
print("result: \(result), error: \(String(describing: error))")
})
})
}
}https://stackoverflow.com/questions/57260914
复制相似问题