ios 如何将图片保存至自定义分组
2014-11-17 12:22
316 查看
一般我们照片存储都是直接存储在相机胶卷中,调用下面的方法就可以了:
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
但是如果我们想将图片存储到自定义的分组该怎么做呢,我查了一下ALAssetsLibrary的文档,发现没有相应的方法,好吧,google一下,发现了一篇文章:点击打开链接。
作者的解决思路如下:
1.将图片先保存到相机胶卷中;
2.在相机胶卷中找到这个图片所创建的AlAsset;
3.将此AlAsset添加到你需要存放的自定义分组中。
主要代码如下所示,注释也很清楚了:
保存照片至相册:
增加图片至自定义相册:
如何添加分组:
[[DataCenter defaultAssetsLibrary] addAssetsGroupAlbumWithName:[alertView textFieldAtIndex:0].text
resultBlock:resulBlock
failureBlock:nil];
转载自:http://www.2cto.com/kf/201307/231293.html
void UIImageWriteToSavedPhotosAlbum (
UIImage *image,
id completionTarget,
SEL completionSelector,
void *contextInfo
);
但是如果我们想将图片存储到自定义的分组该怎么做呢,我查了一下ALAssetsLibrary的文档,发现没有相应的方法,好吧,google一下,发现了一篇文章:点击打开链接。
作者的解决思路如下:
1.将图片先保存到相机胶卷中;
2.在相机胶卷中找到这个图片所创建的AlAsset;
3.将此AlAsset添加到你需要存放的自定义分组中。
主要代码如下所示,注释也很清楚了:
保存照片至相册:
-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock { //write the image data to the assets library (camera roll) [self 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:(SaveImageCompletion)completionBlock { __block BOOL albumWasFound = NO; //search all photo albums in the library [self 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 [self assetForURL: assetURL resultBlock:^(ALAsset *asset) { //add photo to the target album [group addAsset: asset]; //run the completion block completionBlock(nil); } failureBlock: completionBlock]; //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 = self; //create new assets album [self 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(nil); } failureBlock: completionBlock]; } failureBlock: completionBlock]; //should be the last iteration anyway, but just in case return; } } failureBlock: completionBlock]; }
如何添加分组:
[[DataCenter defaultAssetsLibrary] addAssetsGroupAlbumWithName:[alertView textFieldAtIndex:0].text
resultBlock:resulBlock
failureBlock:nil];
转载自:http://www.2cto.com/kf/201307/231293.html
相关文章推荐
- 【iOS开发】---- 如何将图片保存至自定义分组
- 【iOS开发】---- 如何将图片保存至自定义分组
- 如何将图片保存至自定义分组
- 如何将图片保存至自定义分组
- 如何将图片保存至自定义分组
- 如何将图片保存至自定义分组
- iOS 开发: 自定义相册, 保存多张图片到自定义相册中
- ios-Swift中如何保存图片到本地
- iOS中将图片保存到自定义相册中
- iOS uitableview自定义相册(实现拍照并保存在指定目录以相册的形式展示图片)
- IOS 如何保存图片到相册实例
- iOS自定义Button如何在按钮中把文字放左边,图片放右边
- IOS开发中如何自定义导航栏的按钮,文字和背景图片
- 如何使用js将canvas保存为图片文件,并且可以自定义文件名
- iOS 保存图片到自定义相册
- iOS---如何截图,如何将图片保存到相册
- IOS 如何保存图片到document实例
- iOS---如何截图,如何将图片保存到相册
- 通过扫描图片得到的HRGN,创建不规则窗体,如何保存HRGN到文件,避免每次扫描
- Access中以文字方式保存文件名的图片如何在报表中预览