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)