【问题标题】:PHPhotoLibrary error: content editing in performChanges results in error "The operation couldn’t be completed. (Cocoa error -1.)"PHPhotoLibrary 错误:performChanges 中的内容编辑导致错误“无法完成操作。(Cocoa 错误 -1。)”
【发布时间】:2017-08-18 01:21:15
【问题描述】:

这是我在一个全新的 swift 应用程序的 viewDidAppear 上运行的代码。 info.plist 使用 Privacy - Photo Library Usage Description 键正确设置。

我得到的错误是The operation couldn’t be completed. (Cocoa error -1.)

我可以创建新资产、删除资产、收藏资产和还原资产...但尝试编辑其内容会导致此错误。我一直在尝试在互联网上搜索有关如何解决此问题的任何线索(堆栈溢出、WWDC 视频、谷歌的 第二 页、必应甚至 yahoo)。

PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) in

    if status != PHAuthorizationStatus.authorized { return }

    let results = PHAsset.fetchAssets(with: nil)
    guard let asset = results.firstObject else { return }

    if asset.canPerform(.content) {

        let inputOptions = PHContentEditingInputRequestOptions()
        inputOptions.isNetworkAccessAllowed = true
        inputOptions.canHandleAdjustmentData = { (asjustmentData) -> Bool in
            return false
        }

        asset.requestContentEditingInput(with: inputOptions, completionHandler: { (input, info) in
            guard let input = input else { return }

            guard let inputImageURL = input.fullSizeImageURL,
            let inputImage = CIImage(contentsOf: inputImageURL) else { return }

                guard let eagle = EAGLContext(api: .openGLES2) else { return }
            let context = CIContext(eaglContext: eagle)
            guard let outputImageRef = context.createCGImage(inputImage, from: inputImage.extent) else { return }
            let outputImage = UIImage(cgImage: outputImageRef, scale: 1.0, orientation: .right)

            let output = PHContentEditingOutput(contentEditingInput: input)

            guard let imageData = UIImageJPEGRepresentation(outputImage, 0.9) else { return }
                try! imageData.write(to: output.renderedContentURL, options: .atomic)

            output.adjustmentData = PHAdjustmentData(formatIdentifier: "myFormat", formatVersion: "1.0", data: Data())

            DispatchQueue.main.async {
                PHPhotoLibrary.shared().performChanges({

                    let request = PHAssetChangeRequest(for: asset)
                    request.contentEditingOutput = output

                }, completionHandler: { (success, error) in
                    print(success ? "It worked" : "It failed with error:")
                    print(error?.localizedDescription ?? "No Errors!")
                })
            }
        })
    }
}

注意在恢复时,iOS“你想编辑”对话框出现,但是上面的代码没有出现对话框并且应用程序不会崩溃。

【问题讨论】:

  • 您好。我也找不到这个讨厌的错误的解决方法。我想知道你是否有幸克服了这个问题。你愿意分享吗?
  • @user1244109 不走运。解决方法(保存新图像)不如编辑图像好。

标签: ios swift swift3 photosframework photokit


【解决方案1】:

请试试这个。

let inputImage = CIImage(contentsOf: inputImageURL) else { return }

// additional code
inputImage = inputImage.applyingOrientation(input.fullSizeImageOrientation)

将“右”改为“上”。

// before
let outputImage = UIImage(cgImage: outputImageRef, scale: 1.0, orientation: .right)

// after
let outputImage = UIImage(cgImage: outputImageRef, scale: 1.0, orientation: .up)

【讨论】:

    【解决方案2】:

    我遇到了同样的错误。对我有用的修复是确保传递给PHAdjustmentDataData 实际上有一些内容。换句话说,如果它为空,则操作失败。

    就我而言,我不需要存储任何真实数据,所以我选择了这个:

    // ...
    let output = PHContentEditingOutput(contentEditingInput: input)
    output.adjustmentData = PHAdjustmentData(
      formatIdentifier: "customImport",
      formatVersion: "1",
      data: "?".data(using: .utf8)!
    )
    // ...
    

    【讨论】:

      【解决方案3】:

      尝试检查写入output.renderedContentURL 的数据。 我得到代码是因为写数据不是图像。

      【讨论】:

        猜你喜欢
        • 2018-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-03
        • 2013-09-23
        • 1970-01-01
        • 2017-06-04
        • 2023-03-29
        相关资源
        最近更新 更多