在我的应用程序中,我有一段代码,其中需要说明PHLivePhoto类型的对象,并将其转换为UIImage。我认为这与PHAsset、PHAssetResource或PHImageManager有关,但不清楚如何执行转换。如何从PHLivePhoto -> UIImage转换的好方法是什么?谢谢你的帮助!
if let livePhoto = object as? PHLivePhoto {
let livePhotoResources = PHAssetResource.assetResources(for: livePhoto)
print("\(livePhotoResources)")
// imageBucket is of type UIImage[]
// livePhoto is of type PHLivePhoto but I don't know how to convert this to type PHAsset
viewModel.imageBucket.append(convertImageAsset(asset: **WHAT_DO_I_INSERT_HERE**))
}..。
func convertImageAsset(asset: PHAsset) -> UIImage {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var tmpImage = UIImage()
option.isSynchronous = true
manager.requestImage(
for: asset,
targetSize: CGSize(width: asset.pixelWidth, height: asset.pixelHeight),
contentMode: .aspectFit,
options: option,
resultHandler: {(result, info)->Void in
tmpImage = result!
})
return tmpImage
}在以下方面的成果:
[<PHAssetResource: 0x28225cdc0> {
type: photo
uti: public.heic
filename: IMG_5442.heic
asset: (null)
locallyAvailable: YES
fileURL: file:///private/var/mobile/Containers/Data/Application/2FB56305-7600-4A8E-9C67-A71B4A4A9607/tmp/live-photo-bundle/75FD3D97-F13E-4F79-A6E9-F0743D443FDD.pvt/IMG_5442.HEIC
width: 0
height: 0
fileSize: 0
analysisType: unavailable
cplResourceType: Unknown
isCurrent: NO
isInCloud: NO
}, <PHAssetResource: 0x28226bc00> {
type: video_cmpl
uti: com.apple.quicktime-movie
filename: IMG_5442.mov
asset: (null)
locallyAvailable: YES
fileURL: file:///private/var/mobile/Containers/Data/Application/2FB56305-7600-4A8E-9C67-A71B4A4A9607/tmp/live-photo-bundle/75FD3D97-F13E-4F79-A6E9-F0743D443FDD.pvt/IMG_5442.MOV
width: 0
height: 0
fileSize: 0
analysisType: unavailable
cplResourceType: Unknown
isCurrent: NO
isInCloud: NO
}]发布于 2022-01-22 00:06:21
您需要使用PHAsset来获取资产,然后从PHImageManager.default()请求图像数据。
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true)
guard let assetIdentifier = results.first?.assetIdentifier else {
return
}
if let phAsset = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil).firstObject {
PHImageManager.default().requestImageDataAndOrientation(for: phAsset, options: nil) { [weak self] data, _, _, _ in
if let data = data, let image = UIImage(data: data) {
self?.viewModel.imageBucket.append(image)
}
}
}
}要获得assetIdentifier,需要使用共享照片库创建PHPickerConfiguration对象。创建没有照片库的配置只提供资产数据,而不包括资产标识符。
var configuration = PHPickerConfiguration(photoLibrary: .shared())
// Set the filter type according to the user’s selection. .images is a filter to display images, including Live Photos.
configuration.filter = .images
// Set the mode to avoid transcoding, if possible, if your app supports arbitrary image/video encodings.
configuration.preferredAssetRepresentationMode = .current
// Set the selection limit.
configuration.selectionLimit = 1
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)发布于 2022-07-07 15:39:05
接受的答案的问题是,获取PHAsset将需要访问照片库,而PHPickerViewController的主要优点之一是能够在不请求权限的情况下获取照片,并且完全避免了所有相关的边缘情况。
因此,另一种获取实时照片的方法是:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
for result in results {
// Live photos
if result.itemProvider.canLoadObject(ofClass: PHLivePhoto.self) {
result.itemProvider.loadObject(ofClass: PHLivePhoto.self, completionHandler: { livePhoto, error in
let resources = PHAssetResource.assetResources(for: livePhoto as! PHLivePhoto)
let photo = resources.first(where: { $0.type == .photo })!
let imageData = NSMutableData()
PHAssetResourceManager.default().requestData(for: photo, options: nil, dataReceivedHandler: { data in
imageData.append(data)
}, completionHandler: { error in
_ = UIImage(data: imageData as Data)!
})
})
}
}
}https://stackoverflow.com/questions/70790836
复制相似问题