ios 保存图片到自己创建的相册

在QQ群里面看到有人问保存图片到自定义相册的办法:

直接上代码:

#import <Foundation/Foundation.h>
#import <AssetsLibrary/AssetsLibrary.h>

typedef void(^SaveImageCompletion)(NSError* error);

@interface ALAssetsLibrary(CustomPhotoAlbum)

-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;
-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;

@end
#import "ALAssetsLibrary+CustomPhotoAlbum.h"

@implementation ALAssetsLibrary(CustomPhotoAlbum)

-(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];

}

@end

使用的时候只要调用:

-(void)saveImage:(UIImage*)image toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;即可……
时间: 2025-01-08 16:30:31

ios 保存图片到自己创建的相册的相关文章

IOS 保存图片至相册

IOS 保存图片至相册 应用中有时我们会有保存图片的需求,如利用UIImagePickerController用IOS设备内置的相机拍照,或是有时我们在应用程序中利用UIKit的 UIGraphicsBeginImageContext,UIGraphicsEndImageContext,UIGraphicsGetImageFromCurrentImageContext方法创建一张图像需要进行保存. IOS的UIKit Framework提供了UIImageWriteToSavedPhotosAl

iOS中plist的创建,数据写入与读取

iOS中plist的创建,数据写入与读取功能创建一个test.plist文件,textInput作为输入,displayLabel作为显示,有一个按钮来触发保持程序triggerStorage: -(void)triggerStorage { displayLabel.text = textInput.text; NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)

蜗牛爱课- iOS中plist的创建,数据写入与读取

iOS中plist的创建,数据写入与读取功能创建一个test.plist文件-(void)triggerStorage{    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);    NSString *path=[paths    objectAtIndex:0];      NSString *filename=[path stringByAppendin

IOS中UIViewController的创建

IOS程序中,一个应用默认只有一个UIWindow.UIWindow是比较特殊的UIView控件,也是程序启动后创建的第一个UIView对象,为了透彻的了解UIWindow和UIViewController之间的关系,以及UIViewController的创建,从新建一个EmptyApplication工程开始. Empty的工程,默认的目录结构中只有AppDelegate类,这个类是Application对象的委托,会在程序不同的生命周期中回调不同的方法(关于application生命周期的话

iOS开发多线程篇—创建线程

iOS开发多线程篇—创建线程 一.创建和启动线程简单说明 一个NSThread对象就代表一条线程 创建.启动线程 (1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; [thread start]; // 线程一启动,就会在线程thread中执行self的run方法 主线程相关用法 + (NSThread *)mainThread; // 获得主线程 -

iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.Library包含Caches.Preferences目录.               上面的完整路径为:用户->资源库->Applicat

iOS开发 纯代码创建UICollectionView

转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xib和StoryBoard创建UICollectionView项目工程的伙伴,需要转换使用纯代码来实现,想避免碰更多的壁,就需要认真了解创建UICollectionView过程了.创建UICollectionView比创建UITableView更加复杂,初始化方式也是相对奇特.以下是使用纯代码创建UI

iOS 使用interface builder 创建太复杂的constrains时容易产生crash

今天写程序,遇到了crash,在界面初始化时不会有,想切换到别的tab页就报错了.主要内容如下: Cannot find an outgoing row head for incoming head UIImageView:0x156caec0.Width{id: 103}, which should never happen. 查了一晚上也没有查出真正原因,最后从xib中删除了2个view,系统正常了.我想,大概使用了过多的不确定constraint,这些constraint对于人脑来说是可以

iOS 保存图片到相册

保存图片到相册 - (IBAction)save_clcik:(UIButton *)sender { if (self.imgView.image==nil) { [SVProgressHUD showWithStatus:@"正在努力加载中..."]; return; } // 将图片写入到相册() UIImageWriteToSavedPhotosAlbum(self.imgView.image, self, @selector(image:didFinishSavingWith