iOS 开发: 自定义相册, 保存多张图片到自定义相册中
2015-04-21 16:01
501 查看
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~ 搞掂~ 有问题可以评论~ 一起探讨~
相关文章推荐
- iOS中将图片保存到自定义相册中
- iOS开发小技巧--实现将图片保存到本地相册
- iOS uitableview自定义相册(实现拍照并保存在指定目录以相册的形式展示图片)
- 关于iOS开发中长按保存图片到相册
- iOS开发笔记之保存图片到相册
- iOS开发之保存图片到手机相册
- 【iOS开发】---- 如何将图片保存至自定义分组
- iOS开发之保存图片到手机相册
- iOS 开发批量保存图片到相册时丢图片的解决方法
- iOS 保存图片到自定义相册
- iOS开发 系统自带保存图片到相册的方法
- 【iOS开发】---- 如何将图片保存至自定义分组
- IOS开发之——保存图片到相册的功能实现
- iOS开发截屏并保存图片到本地相册以及长按保存图片到本地
- iOS开发技巧之:相册中的GIF图片的读取与保存
- iOS 开发批量保存图片到相册时丢图片的解决方法
- iOS开发 - 保存图片到相册和保存屏幕截图到相册
- IOS开发——保存图片到相册
- iOS开发-OC之图片保存到相册
- iOS开发 长按图片保存到相册