iOS 开发: 自定义相册, 保存多张图片到自定义相册中

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~ 搞掂~ 有问题可以评论~ 一起探讨~

时间: 2024-08-24 04:13:06

iOS 开发: 自定义相册, 保存多张图片到自定义相册中的相关文章

从零开始学ios开发(十二):Table Views(中)UITableViewCell定制

我们继续学习Table View的内容,这次主要是针对UITableViewCell,在前一篇的例子中我们已经使用过UITableViewCell,一个默认的UITableViewCell包含imageView.textLabel.detailTextLabel等属性,但是很多时候这些默认的属性并不能满足需要,其实更多的时候我们想自己制定UITableViewCell的内容,这篇学习的就是制定自己的UITableViewCell. UITableViewCell继承自UIView,因此它可以加载

iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流

上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewController(二) --详解CollectionView各种回调>.UICollectionView之所以强大,是因为其具有自定义功能,这一自定义就不得了啦,自由度非常大,定制的高,所以功能也是灰常强大的.本篇博客就不使用自带的流式布局了,我们要自定义一个瀑布流.自定义的瀑布流可以配置其参数: 每个Cell的边距

iOS开发 文件路径保存

OS文件本地化处理要经过三个步骤,获得文件保存路径.根据文件的属性选择对应的存档方式,存档读档的实现. 一.获得文件保存路径 1."应用程序包": 这里面存放的是应用程序的源文件,包括资源文件和可执行文件. NSString *path = [[NSBundle mainBundle] bundlePath]; 2.Documents: 最常用的目录,iTunes同步该应用时会同步此文件夹中的内容,适合存储重要数据. NSString *path = NSSearchPathForDi

IOS开发——UI进阶篇(二)自定义cell

一.纯代码自定义等高cell 1.创建子控件 2.布局子控件 1.普通 2.Autolayout 3.设置子控件数据 二.字典转模型优化(MJExtension) 三.xib自定义等高cell 四.storyboard自定义等高cell 五.不同类型的cell共存 六.分割线 七.自定义不等高的sell 1. 2. 3. 4.

IOS开发之三级控制器的使用与自定义标签工具栏

三级控制器的概念:UITabBarController- ->(管理) UINavigationController -->(管理) UIViewController 下面笔者就分享一下三级控制器的使用 #import "MainTabbarController.h" #import "ProfileViewController.h" #import "GroupViewController.h" #import "Sear

[IOS 开发] TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式)

第一步: //UserTableViewCell.h这里定义第一种Cell #import <UIKit/UIKit.h> @interface UserTableViewCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *userviewcellicon; @property (weak, nonatomic) IBOutlet UILabel *userviewcellname; @end //U

iOS开发编码建议(Objective-C)(持续更新中)

在开发过程中,我们不仅要去看别人的代码,也要让别人看我们的代码.那么,有一个良好的编码习惯将会非常重要.下面将会罗列使用Objective-C来开发iOS的编码建议. [1] 任意函数长度不得超过50行.(其实很容易就超过50行,这就要考虑代码抽取了.) [2] 任意行代码不能超过80字符.(其实也很容易超过80字符,可以考虑多行显示,比如有多个参数时,可以每个参数放一行.)可以在Xcode中设置超过80个字符的提醒,选中"Page guide at column".设置完之后就会在代

iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流

在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,Cell的最大以及最小高度是在我们的布局文件中是写死的,换句话说也就是不可配置的.为了循序渐进,由浅入深呢,上篇博客暂且那么写.不过那样写太过死板,本来使用起来比较灵活的自定义布局,如果把其配置参数给写死了,就相当于在笼中的猛兽,再厉害不也白扯蛮. 在今天这篇博客中我们要接着上篇博客中的Demo,使其自定义布局

iOS开发系列--C语言之基础知识

概览 当前移动开发的趋势已经势不可挡,这个系列希望浅谈一下个人对IOS开发的一些见解,这个IOS系列计划从几个角度去说IOS开发: C语言 OC基础 IOS开发(iphone/ipad) Swift 这么看下去还有大量的内容需要持续补充,但是今天我们从最基础的C语言开始,C语言部分我将分成几个章节去说,今天我们简单看一下C的一些基础知识,更高级的内容我将放到后面的文章中. 今天基础知识分为以下几点内容(注意:循环.条件语句在此不再赘述): Hello World 运行过程 数据类型 运算符 常用