IOS 第三方程序 调用Camera roll

IOS  获取 Camera roll里的图片 or 视频。

-(void)getImgs{

    dispatch_async(dispatch_get_main_queue(), ^{

        NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];

        ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){

            NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);

            if ([myerror.localizedDescriptionrangeOfString:@"Global denied access"].location!=NSNotFound) {

                NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");

            }else{

                NSLog(@"相册访问失败.");

            }

        };

        

        ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result,NSUInteger index, BOOL *stop){

            if (result!=NULL) {

                

                if ([[result valueForProperty:ALAssetPropertyType]isEqualToString:ALAssetTypePhoto]) {

                    

                    NSString *urlstr=[NSStringstringWithFormat:@"%@",result.defaultRepresentation.url];

                    //图片的url

                    /*result.defaultRepresentation.fullScreenImage//图片的大图

                     result.thumbnail                            //图片的缩略图小图

                     //                    NSRange range1=[urlstr rangeOfString:@"id="];

                     //                    NSString *resultName=[urlstr substringFromIndex:range1.location+3];

                     //                    resultName=[resultName stringByReplacingOccurrencesOfString:@"&ext=" withString:@"."];//格式demo:123456.png

                     */

                    

//                    [self._dataArray addObject:urlstr];

//                    [self getFileInfo:result.defaultRepresentation.url];

                }

                else

                {

                    ALAssetRepresentation *representation = result.defaultRepresentation;

//                    NSLog(@"Video url = %@", representation.url);

//                    NSLog(@"Video thumbnail = %@",representation.fullScreenImage);

                    

                }

                MediaInfo* info = [[MediaInfo alloc] init];

                info.mediaName = @"XXXXXXXXX.jpg";

                NSString* destPath = [Utils cachePathForKey:info thumbnail:NO];

                UIImage* image = [UIImageimageWithCGImage:result.defaultRepresentation.fullResolutionImage];

                NSData *imageData = UIImageJPEGRepresentation(image, 0);
                [imageData writeToFile:destPath atomically:YES];

                NSString *urlstr=[NSStringstringWithFormat:@"%@",result.defaultRepresentation.url];

                NSRange range1=[urlstr rangeOfString:@"id="];

                NSLog(@"%@", [urlstr substringFromIndex:range1.location+3]);

//                [self copyItem:result.defaultRepresentation.url];

            }

        };

        ALAssetsLibraryGroupsEnumerationResultsBlock

        libraryGroupsEnumeration = ^(ALAssetsGroup* group,BOOL* stop){

            

            if (group == nil)

            {

            }
            if (group!=nil) {

                NSString *g=[NSStringstringWithFormat:@"%@",group];//获取相簿的组

                NSLog(@"gg:%@",g);//gg:ALAssetsGroup - Name:Camera Roll, Type:Saved Photos, Assets count:71

                

                NSString *g1=[g substringFromIndex:16 ] ;

                NSArray *arr=[NSArrayarrayWithArray:[g1 componentsSeparatedByString:@","]];

                NSString *g2=[[arr objectAtIndex:0]substringFromIndex:5];

                if ([g2 isEqualToString:@"Camera Roll"]) {

                    g2=@"相机胶卷";

                }

                NSString *groupName=g2;//组的name

                [group enumerateAssetsUsingBlock:groupEnumerAtion];

            }

            

        };

        

        ALAssetsLibrary* library = [[ALAssetsLibraryalloc] init];

        [library enumerateGroupsWithTypes:ALAssetsGroupAll

                              usingBlock:libraryGroupsEnumeration

                            failureBlock:failureblock];

        [library release];      

        [pool release];

    });  
}

可以拿到Photo和Video的缩略图(Thumbnail),屏幕大小的图(fullScreenImage),原图(fullResolutionImage)以及文件url。

无法对camera roll里的源文件进行复制,删除操作,如果有权限可以替换对应的文件。

原文地址:https://www.cnblogs.com/androidwsjisji/p/3027826.html