1、自定义相册(兼容 iOS7 iOS8)
-
- (void)viewDidLoad { //search all photo albums in the library [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) { //compare the names of the albums if ([@"九哒" compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) { //target album is found _isHaveAlbum = YES; return; } if (group==nil && _isHaveAlbum == NO) { //photo albums are over, target album does not exist, thus create it //__weak ALAssetsLibrary* weakSelf = assetsLibrary; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { // iOS 8 code // Create new album. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"九哒"]; } completionHandler:^(BOOL success, NSError *error) { if (!success) { NSLog(@"Error creating album: %@", error); } }]; } else { // iOS 7.x code //create new assets album [assetsLibrary addAssetsGroupAlbumWithName:@"九哒" resultBlock:nil failureBlock: nil]; } //should be the last iteration anyway, but just in case _isHaveAlbum = YES; return; } } failureBlock: nil]; }
具体的解释我已经在上一篇博文(http://www.cnblogs.com/cai-rd/p/4432908.html)中解释, 这里就不多解释了, 上面代码需要补充一个变量 isHaveAblum = NO;
2、把多张照片存入自定义的相册中
-
for(int i = 0; i < 9; i++){ // 利用延时每隔0.1秒存入 一次 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1*i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self saveImage:yourImage toAlbum:@"Rd" withCompletionBlock:^{ NSLog(@"save s img success"); }]; }); }
/** * 保存图片到相册中 * * @param image <#image description#> * @param albumName <#albumName description#> * @param completionBlock <#completionBlock description#> */ -(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(void (^)(void))completionBlock { ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; //write the image data to the assets library (camera roll) [assetsLibrary writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL* assetURL, NSError* error) { //error handling if (error!=nil) { //completionBlock(error); return; } //add the asset to the custom photo album [self addAssetURL: assetURL toAlbum:albumName withCompletionBlock:completionBlock]; }]; } -(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(void (^)(void))completionBlock { ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; __block BOOL albumWasFound = NO; //search all photo albums in the library [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) { //compare the names of the albums if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) { //target album is found albumWasFound = YES; //get a hold of the photo‘s asset instance [assetsLibrary assetForURL: assetURL resultBlock:^(ALAsset *asset) { //add photo to the target album [group addAsset: asset]; //run the completion block completionBlock(); } failureBlock: nil]; //album was found, bail out of the method return; } if (group==nil && albumWasFound==NO) { //photo albums are over, target album does not exist, thus create it __weak ALAssetsLibrary* weakSelf = assetsLibrary; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { // iOS 8 code // Create new album. [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"九哒"]; } completionHandler:^(BOOL success, NSError *error) { if (!success) { NSLog(@"Error creating album: %@", error); } }]; } else { // iOS 7.x code //create new assets album [assetsLibrary addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group) { //get the photo‘s instance [weakSelf assetForURL: assetURL resultBlock:^(ALAsset *asset) { //add photo to the newly created album [group addAsset: asset]; //call the completion block completionBlock(); } failureBlock: nil]; } failureBlock: nil]; albumWasFound = YES; } //should be the last iteration anyway, but just in case return; } } failureBlock: nil]; }
3、需要引入的头文件
-
#import <AssetsLibrary/ALAsset.h> #import <AssetsLibrary/ALAssetsLibrary.h> #import <AssetsLibrary/ALAssetsGroup.h> #import <AssetsLibrary/ALAssetRepresentation.h> #import <Photos/Photos.h>
ok~ 搞掂~ 有问题可以评论~ 一起探讨~
时间: 2024-11-14 06:02:09