ios - How to use PHAssetResourceRequestOptions progressHandler/PHAssetResourceProgressHandler -
i'm trying retrieve/download video/frames of live photo. api documents there possible scenario live photos store @ icloud. in order retrieve them need declare
let options = phassetresourcerequestoptions() options.networkaccessallowed = true
i'm trying create progress bar while live photo being download. according api, need declare properties:
public var progresshandler: phassetresourceprogresshandler? progress floating-point value indicating progress of download. value of 0.0 indicates download has started, , value of 1.0 indicates download complete.
i haven't found correct way retrieve yet. suggestion?
full code :
let assestresource = phassetresource.assetresourcesforasset(asset) let options = phassetresourcerequestoptions() options.networkaccessallowed = true 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) } })
yes, unfortunately, there apple bug icloud downloads + phassetresourcemanager. following error regardless of asset type:
error: missing resource download context
instead, use phimagemanager. need have unique request each type of 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]; (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(); } }]; } }]; } }
Comments
Post a Comment