iOS开发——系统相册操作

在我们的应用中使用图片、视频,尤其是大量的图片、视频的时候。如果存储在沙盒中,会使得该应用的沙盒文件越来越大。所以,一些图片、视频资源可以放到系统相册中保存。这样也有利于用户查看该类资源。
但是,资源存储在了系统相册,我们对其的访问就受到了一定的限制。具体解决办法如下:
1.把视频、图片存储在系统相册:
ALAssetsLibrary *library = [[ ALAssetsLibrary alloc ] init ];
视频存储: - ( void )writeVideoAtPathToSavedPhotosAlbum:( NSURL *)videoPathURL completionBlock:( ALAssetsLibraryWriteVideoCompletionBlock )completionBlock;
图片存储: - ( void )writeImageDataToSavedPhotosAlbum:( NSData *)imageData metadata:( NSDictionary *)metadata completionBlock:( ALAssetsLibraryWriteImageCompletionBlock )completionBlock 
2.把图片、视频文件从相册中读取出来:
- ( void )getThumImageAndURLForPhotoAndVideo
{
   
__weak XYLocalAlbumViewcontroller *weakSelf = self ;
   
dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT , 0 ), ^{
       
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^( ALAssetsGroup *group, BOOL *stop) {
           
if (group != nil ) {
                [weakSelf.
groupArrays addObject :group];
               
NSLog ( @"[group valueForProperty:ALAssetsGroupPropertyName]: %@" ,[group valueForProperty : ALAssetsGroupPropertyName ]);
               
if ([[group valueForProperty : ALAssetsGroupPropertyName ] isEqualToString : XIAOYIALBUM ]) {
                   
self . collectionViewCellCount = group. numberOfAssets ;
                   
NSLog ( @"self.collectionViewCellCount: %ld" , self . collectionViewCellCount );
                   
                    // UI 的更新记得放在主线程
                    dispatch_async ( dispatch_get_main_queue (), ^{
                        [
self createCollectionViewForAlbum ];
                    });
                }
            }
else {
                [weakSelf.
groupArrays enumerateObjectsUsingBlock :^( id obj, NSUInteger idx, BOOL *stop) {
                   
NSLog ( @"obj----idx: %@---------%lu" ,obj, ( unsigned long )idx);
                   
if ([[( ALAssetsGroup *)obj valueForProperty : ALAssetsGroupPropertyName ] isEqualToString : XIAOYIALBUM ]) {
                        [obj
enumerateAssetsUsingBlock :^( ALAsset *result, NSUInteger index, BOOL *stop) {
                           
if ([result thumbnail ] != nil ) {
                               
// 照片
                               
if ([[result valueForProperty : ALAssetPropertyType ] isEqualToString : ALAssetTypePhoto ]){
                                   
                                   
NSDate *date= [result valueForProperty : ALAssetPropertyDate ];
                                   
//                                UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];
                                   
NSString *fileName = [[result defaultRepresentation ] filename ];
                                   
NSURL *url = [[result defaultRepresentation ] url ];
                                   
int64_t fileSize = [[result defaultRepresentation ] size ];
                                   
                                   
NSLog ( @"date = %@" ,date);
                                   
NSLog ( @"fileName = %@" ,fileName);
                                   
NSLog ( @"url = %@" ,url);
                                   
NSLog ( @"fileSize = %lld" ,fileSize);
                                   
                                   
// UI 的更新记得放在主线程 , 要不然等子线程排队过来都不知道什么年代了 , 会很慢的
                                   
dispatch_async ( dispatch_get_main_queue (), ^{
                                       
                                    });
                                }
                               
// 视频
                               
else if ([[result valueForProperty : ALAssetPropertyType ] isEqualToString : ALAssetTypeVideo ] ){
                                   
                                   
// 和图片方法类似
                                   
NSDate *date= [result valueForProperty : ALAssetPropertyDate ];
                                   
//                                UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];
                                   
NSString *fileName = [[result defaultRepresentation ] filename ];
                                   
NSURL *url = [[result defaultRepresentation ] url ];
                                   
int64_t fileSize = [[result defaultRepresentation ] size ];
                                   
                                   
NSLog ( @"date = %@" ,date);
                                   
NSLog ( @"fileName = %@" ,fileName);
                                   
NSLog ( @"url = %@" ,url);
                                   
NSLog ( @"fileSize = %lld" ,fileSize);
                                }
                            }
                        }];
                    }
                  
                }];
               
            }
        };
       
       
ALAssetsLibraryAccessFailureBlock failureBlock = ^( NSError *error)
        {
           
           
NSString *errorMessage = nil ;
           
           
switch ([error code ]) {
               
case ALAssetsLibraryAccessUserDeniedError :
               
case ALAssetsLibraryAccessGloballyDeniedError :
                    errorMessage =
@" 用户拒绝访问相册 , 请在 < 隐私 > 中开启 " ;
                   
break ;
                   
               
default :
                    errorMessage =
@"Reason unknown." ;
                   
break ;
            }
           
           
dispatch_async ( dispatch_get_main_queue (), ^{
               
UIAlertView *alertView = [[ UIAlertView alloc ] initWithTitle : @" 错误 , 无法访问 !"
                                                                  
message :errorMessage
                                                                 
delegate : self
                                                        
cancelButtonTitle : @" 确定 "
                                                        
otherButtonTitles : nil , nil ];
                [alertView
show ];
            });
        };
       
       
       
ALAssetsLibrary *assetsLibrary = [[ ALAssetsLibrary alloc init ];
        [assetsLibrary
enumerateGroupsWithTypes : ALAssetsGroupAll
                                    
usingBlock :listGroupBlock failureBlock :failureBlock];
    });
}

//获取图片、视频的缩略图
- ( void )getThumbnail
{
    ALAssetsLibrary *assetLibrary = [[ ALAssetsLibrary alloc ] init ];
    for ( int i = 0 ; i < self . dataArray . count ; i++) {
       
// 获取路径
       
NSString *string = self . dataArray [i];
       
NSURL *url = [ NSURL URLWithString :string];
       
// 根据图片 url 获取图片
        [assetLibrary
assetForURL :url resultBlock :^( ALAsset *asset) {
            if (asset != nil) {
                // 缩略图img
                UIImage *image = [ UIImage imageWithCGImage :asset. thumbnail ];
               
//
               
if (image != nil ) {
                    [
self . imagesArray addObject :image];
                   
                   
// 大图不需要
                   
                   
// 判断文件后缀 , 是图片还是视频 , 放到数组中 , 只有 mp4 格式被认为是视频
                   
NSRange range = [string rangeOfString : @"&ext=" ];
                   
NSInteger loc = range. location + range. length ;
                   
NSString *subString = [string substringFromIndex :loc];
                   
NSString *suffix = nil ;
                   
if ([subString isEqualToString : @"mp4" ]) {
                        suffix =
@"mp4" ;
                    }
else {
                        suffix =
@"notMp4" ;
                    }
                    [
self . photoOrVideoArray addObject :suffix];
                   
                    [
self . imageDict setObject :image forKey :string];
                    [self.typeDict setObject:suffix forKey:string];
                }
            }


        }
failureBlock :^( NSError *error) {
            
        }];
       
    }
}

除了删除操作,其他的操作基本都有了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值