(go) Save a picture to an album

Source: Internet
Author: User

1. First, it is relatively simple to save the picture to the system in iOS, the implementation of the following statement can be
1- (Ibaction) Save: (ID) Sender {2 // store pictures to "camera Roll" 3 4 }5 6 // You must call this method if you successfully save the picture to the album, otherwise you will report the parameter out of bounds error 7-(void) Image: (UIImage *) image didfinishsavingwitherror: (Nserror *) error ContextInfo: (void *8 9}           
2. We can also save pictures using the assets Library framework before iOS9
    • First step: Add the Assetslibrary.framework framework
    • Step Two: Import the header file inside the class you want to use #import
    • The third step: Save the network picture to the local
1-(void) Viewdidload {2[Super Viewdidload];34 UIImage *image = [UIImage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:@"Http://pic25.nipic.com/20121209/9252150_194258033000_2.jpg"]]];5[Self saveimage:image];6}7-(void) SaveImage: (uiimage*) Image {8 Alassetslibrary *library =[[Alassetslibrary alloc] init];9[Library Writeimagetosavedphotosalbum:[image Cgimage] Orientation: (alassetorientation) image.imageorientation completionblock:^ (Nsurl *asseturl, Nserror *error) {One  -if (error) {NSLog (@ "Save image fail:%@", error);  ()else{NSLog (@ "Save image succeed.") );  } ]; + }                    

3. However, Apple has abandoned the above framework in IOS9 and recommended using the photos framework
    • You also need to add photos.framework first
    • Post-Import #import <Photos/Photos.h>

Photos.framework frame is very powerful, not only pictures, selfie, Panorama, iOS9 in the new live Photo, and even video, etc., the framework has a detailed division and function settings

Here I want to achieve is the first method can not achieve the function, that is to build an album belonging to this app, this feature will be often encountered, the code is as follows, there are detailed comments:

1-(void) SaveImage {2//Save picture3 __block NSString *assetid =Nil4//1. Save pictures to "camera roll"5 [[Phphotolibrary sharedphotolibrary] performchanges:^{6//Create a new Phassetcreationrequest object7//Returns the string identifier of the Phasset (picture)8 AssetID =[Phassetcreationrequest CreationRequestForAssetFromImage:self.imageView.image]. Placeholderforcreatedasset.localidentifier;9} completionhandler:^ (BOOL success, Nserror *_nullable error) {10//2. Get album objectsPhassetcollection *collection =[Self getcollection];12//3. Add a picture from the camera roll to a new album[[Phphotolibrary Sharedphotolibrary] performchanges:^{Phassetcollectionchangerequest *request =[Phassetcollectionchangerequest changerequestforassetcollection:collection];NSLog (@"%@", [Phasset Fetchassetswithlocalidentifiers:@[assetid] options:nil]);16//Get photo objects based on unique markingsPhasset *asset =[Phasset Fetchassetswithlocalidentifiers:@[assetid] options:nil].firstobject;18//Add pictures to albums19[Request Addassets:@[asset]];completionhandler:^ (BOOL success, Nserror *_nullable error) {NSLog (@"Successfully saved to photo album:%@", Collection.localizedtitle);22}];23}];24}25-(Phassetcollection *) GetCollection {27//Get the albums you've created beforePhfetchresult<phassetcollection *> *collectionresult =[Phassetcollection Fetchassetcollectionswithtype:phassetcollectiontypealbum subtype: Phassetcollectionsubtypealbumregular Options:nil];29for (Phassetcollection *collectionInchCollectionresult) {30if ([Collection.localizedtitle isequaltostring:@"Informed daily"]) {31ReturnCollection32}33}3435//Create a new album (folder) if the album does not exist__block nsstring *collectionid = nil;//__block Modifying the value of a variable outside the block37//This method will not be returned until the album has been created.[[Phphotolibrary Sharedphotolibrary] performchangesandwait:^{ A// new Phassertcollectionchangerequest object to create a new album CollectionID = [ Phassetcollectionchangerequest creationrequestforassetcollectionwithtitle:@ " informed daily "]. Placeholderforcreatedassetcollection.localidentifier;  Error:nil];    [phassetcollection Fetchassetcollectionswithlocalidentifiers:@[collectionid] Options:nil].firstobject;                 /+}

Last Words

The Photos.framework framework is really powerful, but after reading Apple's official documentation, it's really cumbersome to use, and in my opinion the framework is similar to core Data, and every time you do it you have to create your own request. and build an intermediate album in memory to buffer, and all operations, the system is completed in the background.

Another thing to note is: Completionhandler: Do not refresh the foreground view in the closed packet returned!!! This is explicitly mentioned in the Apple documentation.

(go) Save a picture to an album

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.