首页 > 代码库 > 代码中怎么新建一个相簿?
代码中怎么新建一个相簿?
转载自: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)) }]; }
代码中怎么新建一个相簿?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。