IOS, ios9

Source: Internet
Author: User

IOS, ios9
 

Import photos. framework before use

Then Import

# Import <Photos/PHPhotoLibrary. h>

# Import <Photos/PHAssetChangeRequest. h>

# Import <Photos/PHImageManager. h>

 

 

Method 1 use the UIImageWriteToSavedPhotosAlbum function to save the image to the album, for example:
- (void)loadImageFinished:(UIImage *)image{    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);}- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);}

The first parameter is the image object to be saved to the album.

The second parameter is the target object for callback after saving.

The third parameter is the method of the target object that is called back after saving. The method declaration should be shown in the Code:

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;

After the fourth parameter is saved, it will be passed back to the contextInfo parameter of the callback method.

Method 2

Use the ALAssetsLibrary class in the AssetsLibrary framework. The Code is as follows:

- (void)loadImageFinished:(UIImage *)image{    __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];    [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {        NSLog(@"assetURL = %@, error = %@", assetURL, error);        lib = nil;    }];}

The writeImageToSavedPhotosAlbum: metadata: completionBlock: Method implementation of the ALAssetsLibrary class is used. The first parameter is a CGImageRef object, indicating the image to be passed in. The second parameter is some attributes of the image, so nil is input because it is not set here. The last completionBlock is the callback after the storage is complete. In this callback, you can obtain the path of the saved image and the error message when the storage fails.

Note: When using this class, you need to import AssetsLibrary. framework. In addition, this class can be used in iOS4.0 or above, but it is marked as obsolete after iOS9.0. We recommend that you use PHPhotoLibrary in the Photos. framework instead, which is the third method described below.

Method 3

Use the PHPhotoLibrary class of the Photos framework to save to the album. The Code is as follows:

-(Void) loadImageFinished :( UIImage: ^ (BOOL success, NSError * _ Nullable error) {NSLog (@ "success = % d, error = % @", success, error) ;}];}

In this example, call the performChanges: completionHandler: Method of the PHPhotoLibrary class, and then use the creationRequestForAssetFromImage: Method of the PHPhotoLibrary class in its changeBlock to pass in an image object to save it to the album. Then, the completionHandler will tell us whether the operation is successful.

Advanced usage: Get the image object saved to the album

Some may need to get the PHAsset object of the image after saving the album for subsequent operations (a friend encountered such a problem yesterday ). The above example is improved here. After PHAssetChangeRequest is created, the localIdentifier attribute of its placeholderForCreatedAsset attribute is saved to an array, wait until the operation is complete and then use this array to find the newly added image object. See the following Chestnuts:

-(Void) loadImageFinished :( UIImage *) image {NSMutableArray * imageIds = [NSMutableArray array]; [[PHPhotoLibrary sharedPhotoLibrary] Using mchanges: ^ {// write the image to the photo album PHAssetChangeRequest * req = [PHAssetChangeRequest creationRequestForAssetFromImage: image]; // record the local ID and obtain the image object [imageIds addObject: req. placeholderForCreatedAsset. localIdentifier];} completionHandler: ^ (BOOL success, NSError * _ Nullable error) {NSLog (@ "success = % d, error = % @", success, error ); if (success) {// obtain the image object _ block PHAsset * imageAsset = nil; PHFetchResult * result = [PHAsset fetchAssetsWithLocalIdentifiers: imageIds options: nil]; [result enumerateObjectsUsingBlock: ^ (PHAsset * _ Nonnull obj, NSUInteger idx, BOOL * _ Nonnull stop) {imageAsset = obj; * stop = YES;}]; if (imageAsset) {// load image data [[PHImageManager defaultManager] placement: imageAsset options: nil resultHandler: ^ (NSData * _ Nullable imageData, NSString * _ Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _ Nullable info) {NSLog ("imageData =%@", imageData) ;}}}] ;}}
Summary

The first method is the most commonly used method. It is very convenient to use. You can pass in the UIImage without worrying about different iOS versions. The only drawback is that the image cannot be found.

The second method is added after iOS4, which is not recommended after iOS9. He also provides an intuitive way to save images, and can also obtain the corresponding image path after being saved.

The third method is added after iOS8. Its usage is a little more complex, but it allows batch operations, such as adding, modifying, and deleting. This method is recommended for more complex operations.

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.