Roi*_*lia 6 objective-c progress ios swift phasset
我正在尝试检索/下载Live Photo的视频/帧.至于API文档,有可能将Live Photos存储在iCloud中.为了检索它们,你需要声明
let options = PHAssetResourceRequestOptions()
options.networkAccessAllowed = true
Run Code Online (Sandbox Code Playgroud)
我正在尝试在下载Live Photo时创建进度条.根据API,您需要声明此属性:
public var progressHandler: PHAssetResourceProgressHandler?
progress
A floating-point value indicating the progress of the download.
A value of 0.0 indicates that the download has just started,
and a value of 1.0 indicates the download is complete.
Run Code Online (Sandbox Code Playgroud)
我还没有找到正确的方法来检索那些.有什么建议吗?
完整代码:
let assestResource = PHAssetResource.assetResourcesForAsset(asset)
let options = PHAssetResourceRequestOptions()
options.networkAccessAllowed = true
for assetRes in assestResource {
print(assetRes.type.rawValue)
if (assetRes.type == .PairedVideo) {
print("imageTaken")
manager.writeDataForAssetResource(assetRes, toFile: documentsURL, options: options, completionHandler: { (error) -> Void in
if error == nil
{
}
else
{
print(error)
}
})
Run Code Online (Sandbox Code Playgroud)
是的,很不幸,iCloud下载+ PHAssetResourceManager出现一个Apple错误。无论资产类型如何,都会出现以下错误:
错误:缺少资源下载上下文
而是使用PHImageManager。对于每种类型的PHAsset,您都需要一个唯一的请求:
- (void)downloadAsset:(PHAsset *)asset toURL:(NSURL *)url completion:(void (^)(void))completion
{
if (asset.mediaType == PHAssetMediaTypeImage && (asset.mediaSubtypes & PHAssetMediaSubtypePhotoLive))
{
PHLivePhotoRequestOptions *options = [PHLivePhotoRequestOptions new];
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestLivePhotoForAsset:asset targetSize:CGSizeZero contentMode:PHImageContentModeAspectFill options:options resultHandler:^(PHLivePhoto * _Nullable livePhoto, NSDictionary * _Nullable info) {
if ([info objectForKey:PHImageErrorKey] == nil)
{
NSData *livePhotoData = [NSKeyedArchiver archivedDataWithRootObject:livePhoto];
if ([[NSFileManager defaultManager] createFileAtPath:url.path contents:livePhotoData attributes:nil])
{
NSLog(@"downloaded live photo:%@", url.path);
completion();
}
}
}];
}
else if (asset.mediaType == PHAssetMediaTypeImage)
{
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
if ([info objectForKey:PHImageErrorKey] == nil
&& [[NSFileManager defaultManager] createFileAtPath:url.path contents:imageData attributes:nil])
{
NSLog(@"downloaded photo:%@", url.path);
completion();
}
}];
}
else if (asset.mediaType == PHAssetMediaTypeVideo)
{
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
if ([info objectForKey:PHImageErrorKey] == nil)
{
exportSession.outputURL = url;
NSArray<PHAssetResource *> *resources = [PHAssetResource assetResourcesForAsset:asset];
for (PHAssetResource *resource in resources)
{
exportSession.outputFileType = resource.uniformTypeIdentifier;
if (exportSession.outputFileType != nil)
break;
}
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if (exportSession.status == AVAssetExportSessionStatusCompleted)
{
NSLog(@"downloaded video:%@", url.path);
completion();
}
}];
}
}];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3717 次 |
| 最近记录: |