2

在 iOS 15.0 中运行良好,但升级到 iOS 15.1 后,代码无法运行。

_ = PHLivePhoto.request(withResourceFileURLs: [pairedVideoURL, pairedImageURL], placeholderImage: nil, targetSize: CGSize.zero, contentMode: PHImageContentMode.aspectFit, resultHandler: { (livePhoto: PHLivePhoto?, info: [AnyHashable : Any]) -> Void in

    if let isDegraded = info[PHLivePhotoInfoIsDegradedKey] as? Bool, isDegraded {

        return

    }

    DispatchQueue.main.async {

        completion(livePhoto, (pairedImageURL, pairedVideoURL))

    }

})

任何人都可以帮忙吗?

4

2 回答 2

2

我有同样的问题: isPHLivePhoto.request失败。PHPhotosErrorKey error 3302PHPhotosErrorInvalidResource

经过一番研究,发现从IOS 15.1开始,"{MakerApple}"包括Live Photo的id在内的元数据字段停止写入图像文件元数据,因此LivePhoto验证失败,因为图像和电影中的标识符需要相同livePhoto 的一部分。

在尝试了不同的事情之后,发现IOS没有"{MakerApple}"在JPEG图像上写入元数据。它只对 Apple 自己的文件格式执行此操作。(即heic)。(从 IOS 15.1 开始)

要解决此问题,请将您的图像部分编码为heic"{MakerApple}"元数据将被保留。您可以通过以下方式实现此目的:

let assetIdentifier = UUID()

guard
    let destintation = CGImageDestinationCreateWithURL(imageURL as CFURL, UTType.heic.identifier as CFString, 1, nil),
    let data = staticUIImage!.heic,
    let imageSource = CGImageSourceCreateWithData(data as CFData, nil)
else {
    return
}

var metadata = CGImageSourceCopyProperties(imageSource, nil) as! [String : Any]
let makerNote = ["17" : assetIdentifier.uuidString]
metadata[String(kCGImagePropertyMakerAppleDictionary)] = makerNote
CGImageDestinationAddImageFromSource(destintation, imageSource, 0, metadata as CFDictionary)
CGImageDestinationFinalize(destintation)

为了获取数据,我在这里使用了Leo Dabusheic提供的 UIImage 扩展

于 2021-11-05T09:44:41.990 回答
-1
    func addAssetID(_ assetIdentifier: String, toImage imageURL: URL, saveTo destinationURL: URL) -> URL? {
        
        let kFigAppleMakerNote_AssetIdentifier = "17"
        
        let image = UIImage(contentsOfFile: imageURL.path)
        let imageRef = image?.cgImage
        let imageMetadata = [kCGImagePropertyMakerAppleDictionary: [kFigAppleMakerNote_AssetIdentifier: assetIdentifier]]

        let cfUrl = destinationURL as CFURL

        let dest = CGImageDestinationCreateWithURL(cfUrl, kUTTypeJPEG, 1, nil)
        CGImageDestinationAddImage(dest!, imageRef!, imageMetadata as CFDictionary)
        _ = CGImageDestinationFinalize(dest!)
        
        return destinationURL
    }
于 2021-11-04T15:00:21.883 回答