首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift: PHAssetChangeRequest对HEIC图像失败

Swift: PHAssetChangeRequest对HEIC图像失败
EN

Stack Overflow用户
提问于 2019-07-29 20:24:09
回答 1查看 900关注 0票数 4

我想要对来自图库的图像执行编辑,例如,JEPG压缩(由iPhone摄像机拍摄),但是当输入图像是HEIC图像但与JPEG图像一起工作时,它会失败。

我通过PHAsset方法检索UIImagePickerController对象中的图像:

代码语言:javascript
复制
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
            self.asset = asset
        }

        //...
    } 

此函数编辑选定的图像:

代码语言:javascript
复制
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上使用。

  • 注1:使用EXIF查看器,您可以找到图像是否为HEIC,或者在选择图像并单击“编辑图像”按钮后,您可以在图像预览的顶部看到全名。
  • 注2:由于某种原因,当我将HEIC映像从iPhone发送到Mac并将其发送回iPhone时,它将工作在新的副本上,该副本仍然是HEIC,并保留原来的图像方向。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-02 09:39:56

如下所示,如下所示:创建一个CGImageDestination,在.renderedContentURL上编写输出,执行the PHAssetChangeRequest()

代码语言:javascript
复制
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))")
            })

        })

    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57260914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档