码迷,mamicode.com
首页 > 其他好文 > 详细

代码中怎么新建一个相簿?

时间:2014-12-25 16:21:16      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

 

转载自:http://www.cocoachina.com/bbs/read.php?tid=143383

我的创建相册代码:

- (void)saveToAlbumWithMetadata:(NSDictionary*)metadata
                      imageData:(NSData*)imageData
                customAlbumName:(NSString*)customAlbumName
                completionBlock:(void(^)(void))completionBlock
                   failureBlock:(void(^)(NSError*error))failureBlock
{
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
//接下来是疯狂的 block
void(^AddAsset)(ALAssetsLibrary *, NSURL*) = ^(ALAssetsLibrary *assetsLibrary, NSURL*assetURL) {
//1 遍历 AssetsLibrary 整个相册库 [assetsLibrary assetForURL:assetURL resultBlock:
^(ALAsset *asset) {
       //2 遍历 ALAssetsGroup 相册库中文件夹 [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:
^(ALAssetsGroup *group, BOOL*stop) {
//3 找到 customAlbumName 对应的相册库文件夹
if([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customAlbumName]) { [group addAsset:asset]; if(completionBlock) { completionBlock(); } } } failureBlock:^(NSError*error) { if(failureBlock) { failureBlock(error); } }]; } failureBlock:^(NSError*error) { if(failureBlock) { failureBlock(error); } }]; };

//把照片写入相册 [assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:
^(NSURL*assetURL, NSError*error) { if(customAlbumName) { [assetsLibrary addAssetsGroupAlbumWithName:customAlbumName resultBlock:^(ALAssetsGroup *group) { if(group) { [assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) { [group addAsset:asset]; if(completionBlock) { completionBlock(); } } failureBlock:^(NSError*error) { if(failureBlock) { failureBlock(error); } }]; }else{ AddAsset(assetsLibrary, assetURL); } } failureBlock:^(NSError*error) { AddAsset(assetsLibrary, assetURL); }]; }else{ if(completionBlock) { completionBlock(); } } }]; } - (void)saveImage:(UIImage*)image{ ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; NSMutableArray*groups=[[NSMutableArrayalloc]init]; ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL*stop) { if(group) { [groups addObject:group]; } else { BOOLhaveHDRGroup = NO; for(ALAssetsGroup *gp in groups) { NSString*name =[gp valueForProperty:ALAssetsGroupPropertyName]; if([name isEqualToString:@"Flickr墙纸"]) { haveHDRGroup = YES; } } if(!haveHDRGroup) { //do add a group named "HDR" [assetsLibrary addAssetsGroupAlbumWithName:@"Flickr墙纸" resultBlock:^(ALAssetsGroup *group) { [groups addObject:group]; } failureBlock:nil]; haveHDRGroup = YES; } } }; //创建相簿 [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil];

//在我的"扫一扫"代码中,生成的二维码图片存到新创建的相薄后显示全黑,后同事指点原来是下边红色方法的问题,后换了个方法实现了: UIImageJPEGRepresentation(image,1) [self saveToAlbumWithMetadata:nilimageData:UIImagePNGRepresentation(image) customAlbumName:@"Flickr墙纸"completionBlock:^ { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存成功"message:@"若想马上启用该墙纸,请进入图库找到该墙纸并设置为墙纸即可,感谢您的使用。"delegate:nilcancelButtonTitle:@""otherButtonTitles: nil]; [alert show]; } failureBlock:^(NSError*error) { UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"保存失败"message:error.description delegate:nilcancelButtonTitle:@"知道了"otherButtonTitles: nil]; [alert show]; //面这个方法可判断是否是因为用户拒绝访问地址所致,如果是中文环境返回 的是“用户拒绝访问” if (([myerror.localizedDescription rangeOfString:@"User denied access"].location!=NSNotFound )|| ([myerror.localizedDescription rangeOfString:@"用户拒绝访问"].location!=NSNotFound)) }]; }

 

代码中怎么新建一个相簿?

标签:

原文地址:http://www.cnblogs.com/allanliu/p/4184791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!