1, C language function manner
Note: UIImageWriteToSavedPhotosAlbum method must implement the proxy method, otherwise it will collapse.
// Parameter 1: Picture Object // Parameter 2: successful method of binding target // Parameter 3: After successfully calling the method // Parameter 4: need to pass information (parameters after a successful method call) UIImageWriteToSavedPhotosAlbum (self.imageView.image , Self, @selector (Image: didFinishSavingWithError: contextInfo :), nil); #pragma Mark - <save to album> - ( void ) Image: (UIImage *) Image didFinishSavingWithError: (NSError *) error contextInfo :( void * ) contextInfo { NSString * MSG = nil; IF (error) { MSG = @ " save image failed " ; } the else { MSG = @ "Save picture success " ; } }
Method 2: Use framework implemented Photos
2.1 Photos01- basic understanding
PHAsset: a PHAsset the object represents a picture album or a video
PHAssetCollection: a PHAssetCollection target represents an album
If we want to save the image to the camera roll [], we must first ensure PHAsset add a new object, how to operate these objects? Nothing but these objects CRUD.
PHAsset
一个PHAsset对象就代表相册中的一张图片或者一个视频
CRUD PHAssetChangeRequest including pictures / video-related changes in operationsCharles [PHAsset fetchAssets ...];
PHAssetCollection
一个PHAssetCollection 对象就代表一个相册
CRUD PHAssetCollectionChangeRequest album includes all modifications related to the operation
check
[PHAssetCollection fetchAssetCollectionsContainingAsset:...];
2.2 Photos02- save the image to your camera roll
// save the picture to the camera roll] [ /// asynchronous modify operation [[PHPhotoLibrary sharedPhotoLibrary] performChanges: ^ { [PHAssetChangeRequest creationRequestForAssetFromImage: self.imageView.image]; } CompletionHandler: ^ (BOOL Success, the NSError * _Nullable error) { IF (error) { NSLog ( @ " % @ " , @ " save failed " ); } the else { NSLog ( @ " % @ " , @ " saved successfully " ); } }];
2.3 Photos03- create a new album
* Error = NSError nil; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait: ^ { // get app name NSString * title = [NSBundle mainBundle] .infoDictionary [(__ Bridge NSString * ) kCFBundleNameKey]; // create a custom album [] [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle: title]; } error: & error];
2.4 Photos04- inquiry album
NSString *title = [NSBundle mainBundle].infoDictionary[(__bridge NSString*)kCFBundleNameKey]; //查询所有【自定义相册】 PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; PHAssetCollection *createCollection = nil; for (PHAssetCollection *collection in collections) { if ([collection.localizedTitle isEqualToString:title]) { createCollection = collection; break; } } if(createCollection == nil) { // this is not the album corresponding to app created // Create custom albums [a] the NSError * error = nil; __block NSString * createCollectionID = nil; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait: ^ { // Create a custom albums] [ createCollectionID = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle: title] .placeholderForCreatedAssetCollection.localIdentifier; } error: & error]; createCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createCollectionID] options:nil].firstObject; } NSLog(@"%@",createCollection);
2.5 Photos05- save the image to a custom album
// 1. First save the image to the camera roll [] /// synchronized modify operation NSError * error = nil; __block PHObjectPlaceholder * placeholder = nil; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait: ^ { placeholder = [PHAssetChangeRequest creationRequestForAssetFromImage: self.imageView .image] .placeholderForCreatedAsset; } error: & error]; IF (error) { NSLog ( @ " save failed " ); return ; } // 2. has a customized album] [ PHAssetCollection * assetCollection =self.createCollection; IF (assetCollection == nil) { NSLog ( @ " Create Album failed " ); } // 3. just saved to the Camera Roll] [pictures inside references to the custom album] [ [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait: ^ { PHAssetCollectionChangeRequest * requtes = [PHAssetCollectionChangeRequest changeRequestForAssetCollection: assetCollection]; [requtes addAssets: @ [placeholder]]; } error: & error]; IF (error) { NSLog ( @ " save image failed " ); } the else { NSLog (@ " Save Picture Success " ); }