IOS saves images or videos to photolibrary

Source: Internet
Author: User
Tags file url

The APIS for storing images to photo library are similar to those for storing videos to photo library, but they are also different. Images can directly write data to the photo library, while video needs to first save the data to a temporary file and then transfer it to the photo library through the path of the temporary file.

Let's look at the corresponding API:

// These methods can be used to add photos or videos to the saved photos album.// With a UIImage, the API user can use -[UIImage CGImage] to get a CGImageRef, and cast -[UIImage imageOrientation] to ALAssetOrientation.- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef orientation:(ALAssetOrientation)orientation completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock;// The API user will have to specify the orientation key in the metadata dictionary to preserve the orientation of the image- (void)writeImageToSavedPhotosAlbum:(CGImageRef)imageRef metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);// If there is a conflict between the metadata in the image data and the metadata dictionary, the image data metadata values will be overwritten- (void)writeImageDataToSavedPhotosAlbum:(NSData *)imageData metadata:(NSDictionary *)metadata completionBlock:(ALAssetsLibraryWriteImageCompletionBlock)completionBlock __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_4_1);- (void)writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock:(ALAssetsLibraryWriteVideoCompletionBlock)completionBlock;

The first three images are saved. We can see through the parameters that the first one uses the direction we passed in, and the second one can retain the metadata of the image by passing in the metadata of the image, the first two are converting the image into cgimageref and then saving it. The third is inputting nsdata, so the image information can be fully retained, and metadata can also be passed in, if the information contained in the image conflicts with metadata, the metadata will overwrite the metadata contained in the image.

The last one is the video storage API. You can see that the parameter is an nsurl, which only requires the File URL of a local temporary file.

Select an appropriate API for storing images based on your needs. For example, if we obtain a uiimage instance, it is easier to use the first or second one, if we read the image data from a local temporary file, it is more convenient to use the third one directly.

The following is a simple code:

- (void)saveImage:(UIImage*)image{    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];    [assetsLibrary writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error) {        if (error) {            NSLog(@"Save image fail:%@",error);        }else{            NSLog(@"Save image succeed.");        }    }];}

It is troublesome to save the video. You need to first write the video to the local file, then obtain the path of the local temporary file, and then call the fourth API above to write the video to the photo library.

For writing temporary files, I have previously written an article about file read/write. You can check it out.

Here I serve a demo that writes the video of the project resource library to the photo library, so that you can import the video into the simulator for testing in some cases.

The main code is as follows. The whole project can be downloaded at the end of the link:

- (void)save:(NSString*)urlString{    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:urlString]                                completionBlock:^(NSURL *assetURL, NSError *error) {                                    if (error) {                                        NSLog(@"Save video fail:%@",error);                                    } else {                                        NSLog(@"Save video succeed.");                                    }                                }];}

The entire project is attached to the following:

Savevideo2photolibrary (click to download)

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.