iOS choose the processing of pictures and videos on icloud Cloud in albums

Source: Internet
Author: User

About iOS Select an album in icloud cloud tablets and videos recommended: Tzimagepickercontroller source, this is a very reliable album selection of pictures and videos of the library. Of course, you can also write the following problems

Working reason, need to handle access to a video module, in the video selection encountered a not easy to find the bug, the reason is due to the small cell phone memory, and the user opened the album Sync icloud,

Load a picture in

At this point, if the locally available memory is too small, it causes
Delete a picture or video from a local photo album with only thumbnails, and if you want to select this image when the app calls, you need to download it from the icloud cloud.
To get the original image or video.

Below the PO under solution:

If you have previously dealt with the album problem, then the following code is certainly not unfamiliar, is very common two system-level request callback, get the corresponding image, video.

// Get Image [[Phimagemanager Defaultmanager] Requestimagedataforasset:asset options:nil resulthandler:^ (NSData * _Nullable ImageData, NSString * _nullable Datauti, uiimageorientation orientation, nsdictionary * _nullable info) {}]; // Get Video [[Phimagemanager Defaultmanager] Requestplayeritemforvideo:asset options:nil resulthandler:^ (AVPlayerItem * _ Nullable Playeritem, Nsdictionary * _nullable info) {    if  (completion) completion (Playeritem, info);}];

But I often don't notice what the second input options is for,
In fact, the solution comes from PHImageRequestOptions this PHVideoRequestOptions .

All of these two options have a common parameter

@property (nonatomic, assign, getter=isnetworkaccessallowed) BOOL networkaccessallowed; // if necessary would download the image from ICloud (client can monitor or cancel using Progresshandler). Defaults to NO (see Start/stopcachingimagesforassets)

The explanation of the system is also very detailed, if assigned YES , then allows iCloud to get pictures and videos from, by default NO .

Although this problem is not difficult to solve, but often easy to ignore, so record a bit.

Thank you very much for the Tzimagepickercontroller source, this is a very reliable photo album selection of image video library, and is still in maintenance. Interested can chain take a look at the source code, write very well.

Bo Main blog @harwordliu

The following PO is a complete solution to this problem code:

///Get Video- (void) Getvideooutputpathwithasset: (Phasset *) Asset Completion: (void(^) (NSString *OutputPath)) Completion {phvideorequestoptions* Options =[[Phvideorequestoptions alloc] init]; Options.version=phvideorequestoptionsversionoriginal; Options.deliverymode=phvideorequestoptionsdeliverymodeautomatic; Options.networkaccessallowed=YES; [[Phimagemanager Defaultmanager] Requestavassetforvideo:asset options:options resulthandler:^ (avasset* avasset, avaudiomix* Audiomix, nsdictionary*info) {        //NSLog (@ "info:\n%@", Info);Avurlasset *videoasset = (avurlasset*) Avasset; //NSLog (@ "Avasset URL:%@", myasset.url);[self startexportvideowithvideoasset:videoasset completion:completion]; }];}///Get Image-(Phimagerequestid) Getphotowithasset: (Phasset *) Asset Photowidth: (cgfloat) Photowidth completion: (void(^) (UIImage *, Nsdictionary *, BOOL isdegraded)) Completion {Phasset*phasset = (Phasset *) Asset; CGFloat Aspectratio= Phasset.pixelwidth/(cgfloat) phasset.pixelheight; CGFloat pixelwidth= Photowidth *2.0; CGFloat pixelheight= Pixelwidth/Aspectratio; Cgsize imageSize=Cgsizemake (Pixelwidth, pixelheight); //fixed an instantaneous memory problem when getting picturesPhimagerequestoptions *option =[[Phimagerequestoptions alloc] init]; Option.resizemode=Phimagerequestoptionsresizemodefast; Phimagerequestid Imagerequestid= [[Phimagemanager Defaultmanager] Requestimageforasset:asset targetsize:imagesize contentmode: Phimagecontentmodeaspectfill options:option resulthandler:^ (UIImage * _nullable result, nsdictionary *_nullable Info) {BOOL downloadfinined= (! [[Info Objectforkey:phimagecancelledkey] Boolvalue] &&![Info objectforkey:phimageerrorkey]); if(downloadfinined &&result) {            if(completion) completion (Result,info,[[info Objectforkey:phimageresultisdegradedkey] boolValue]); }        //Download image from icloud/download pictures from icloud        if([info Objectforkey:phimageresultisincloudkey] &&!result) {phimagerequestoptions*option =[[Phimagerequestoptions alloc]init]; Option.networkaccessallowed=YES; Option.resizemode=Phimagerequestoptionsresizemodefast; [[Phimagemanager Defaultmanager] Requestimagedataforasset:asset options:option resulthandler:^ (NSData * _nullable imageData, NSString * _nullable Datauti, uiimageorientation orientation, nsdictionary *_nullable Info) {UIImage*resultimage = [UIImage imagewithdata:imagedata scale:0.1]; if(completion) completion (Resultimage,info,[[info Objectforkey:phimageresultisdegradedkey] boolValue]);        }];    }    }]; returnImagerequestid;}

 

iOS choose the processing of icloud tablets and videos in albums

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.