SwiftUI crash with corrupted Live Photo

2 days ago 3
ARTICLE AD BOX

I have a corrupted Live Photo in my Photos library. That photo doesn’t display correctly even in the Photos app. Its thumbnail is completely black, and only when I edit the Live Photo frames can I see which image it is. In my app, I fetch assets and then use PHCachingImageManager to request a UIImage based on the asset. The problem is that it returns corrupted UIImage instead of nil. It doesn’t report any error, even though the data is corrupted. The console prints warnings like these:

createImageBlock:3029: *** ERROR: CGImageBlockCreate {0, 0, 4284, 5712} - data is NULL callDecodeImage:2411: *** ERROR: decodeImageImp failed - NULL _blockArray createImageBlock:3029: *** ERROR: CGImageBlockCreate {0, 0, 4284, 5712} - data is NULL callDecodeImage:2411: *** ERROR: decodeImageImp failed - NULL _blockArray createImageBlock:3029: *** ERROR: CGImageBlockCreate {0, 0, 4284, 5712} - data is NULL callDecodeImage:2411: *** ERROR: decodeImageImp failed - NULL _blockArray

How can I make sure to avoid corrupted photos? I tried checking cgImage as well as verifying that the dimensions are greater than 0, but it doesn’t help. I should note that if I manually change the key frame to another one in the Photos app, everything works normally. I am using SwiftUI Image that accepts UIImage. I tried using requestImageDataAndOrientation and its same behavior. Maybe i can look for start and end bytes but i don't know always file format of photo.

Task { await loadOneMore() } func loadOneMore() async { guard var foto = allFoto.popLast() else { return } let _: Void = await withCheckedContinuation { c in requestImage(for: foto.asset, targetSize: PHImageManagerMaximumSize) { [weak self] image in guard let image else { print("DEBUGPRINT: requestImage guard") return } foto.image = image foto.size = self?.fileSize(asset: foto.asset) ?? 0 self?.workingFoto.insert(foto, at: 0) c.resume() } } } private func requestImage(for asset: PHAsset, targetSize: CGSize, completion: @escaping (UIImage?) -> Void) { let options = PHImageRequestOptions() options.deliveryMode = .highQualityFormat // MARK: - be aware, if opportunistic it will call closure twice options.isNetworkAccessAllowed = true options.resizeMode = .fast options.isSynchronous = false imageManager.requestImage( for: asset, targetSize: targetSize, contentMode: .aspectFill, options: options ) { image, _ in completion(image) } }

Maybe this can be useful, its printed as warning in console. [RM] PHFigDecoder - failed async decode of image at url: file:///var/mobile/Media/DCIM/118APPLE/IMG_8461.HEIC, status code: -16993

Read Entire Article