【问题标题】:Can't generate live photo in iOS 15.1无法在 iOS 15.1 中生成实时照片
【发布时间】:2021-11-04 08:00:00
【问题描述】:

在 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))

    }

})

有人可以帮忙吗?

【问题讨论】:

  • 怎么不起作用?有错误信息吗?与之前的操作系统版本相比,完成处理程序现在返回了什么?

标签: swift photokit phlivephoto


【解决方案1】:

我遇到了同样的问题: PHLivePhoto.request 失败,PHPhotosErrorKey error 3302PHPhotosErrorInvalidResource

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

在尝试不同的事情后,发现IOS没有在JPEG图像上写入"{MakerApple}"元数据。它只对 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)

为了获取heic 数据,我使用了Leo Dabus here 提供的UIImage 扩展

【讨论】:

  • 这为我解决了问题。谢谢!
【解决方案2】:
    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
    }

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2011-10-31
    相关资源
    最近更新 更多