May*_*sam 5 ios swift phasset phphotolibrary
我想对图库中的图像(由 iPhone 相机拍摄)执行编辑,例如JEPG压缩,但当输入图像是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
}
//...
}
Run Code Online (Sandbox Code Playgroud)
此功能编辑选定的图像:
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)")
})
})
}
}
Run Code Online (Sandbox Code Playgroud)
带有示例图像的项目HEIC位于https://github.com/maysamsh/Swift-Playground-EditHEIC
注意 1:使用 EXIF 查看器,您可以查明图像是否为 HEIC,或者在选择图像并单击“编辑图像”按钮后,您可以在图像预览顶部看到全名。
注 2:由于某种原因,当我将 HEIC 图像从 iPhone 发送到 Mac 并将其发送回 iPhone 时,它会在新副本上运行,该副本仍然是 HEIC 并保留原始图像方向。
在这里,它是这样工作的:创建一个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))")
})
})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1148 次 |
| 最近记录: |