繁体   English   中英

从iPhone的照片库获取所有图像

[英]To Get all Images from Photo Library in iphone

我试图从照片库中检索图像并使用AssetsLibrary在我的应用程序中显示。 我有照片的URL路径,但我不知道如何通过照片获取照片。

我的代码如下:

   NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];


   void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
       if(result != nil) {
           if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSLog(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) { [assetsp addObject:asset]; }
                        failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
           } 
       }
   };


   NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
   void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop){
        NSLog(@"hi");
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];

            NSLog(@"Number of assets in group :%d",[group numberOfAssets]);           
        }
   };

   assetGroups = [[NSMutableArray alloc] init];
   ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

   [library enumerateGroupsWithTypes:ALAssetsGroupAll
                          usingBlock:assetGroupEnumerator
                        failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];

此代码是从此处的一些ELCImagePickerController示例中获取的。为简化起见,进行一些修改。

愿这对您有帮助

[self.assetGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) 
     {         
         if(result == nil) 
             return;
         UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
        [assetImageView setContentMode:UIViewContentModeScaleToFill];
        [assetImageView setImage:[UIImage imageWithCGImage:[result thumbnail]]];
     }];    

快乐编码:)

新答案

只需使用

[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]

存储图像,而不是直接存储该资产在存储资产的时间assetsp

如下

void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
       if(result != nil) {
           if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSLog(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) { //Your line
                                                           //[assetsp addObject:asset];
                                                         //My Changed line will store image directly to assetsp
                                                           [assetsp addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
                                                       }
                        failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
           } 
       }
   };

新答案1

尝试使用以下代码将ALAsset数据存储在可变数组中

NSMutableArray *returnArray = [[NSMutableArray alloc] init];

    for(ALAsset *asset in _assets) {

        NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
        [workingDictionary setObject:[asset valueForProperty:ALAssetPropertyType] forKey:@"UIImagePickerControllerMediaType"];
        [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
        [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];

        [returnArray addObject:workingDictionary];

并在UIImageView中获取图像,只需执行此操作

NSDictionary *dict = [info objectAtIndex:i];
        UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
        [imageview setContentMode:UIViewContentModeScaleAspectFit];

我的代码如下:

- (void)viewDidLoad
{
    carousel.type = iCarouselTypeCoverFlow2;
    [super viewDidLoad];
    xy =[[NSMutableArray alloc]init];

    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSLog(@"result is:%@",result);
                NSLog(@"asset URLDictionary is:%@",assetURLDictionaries);
                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [xy addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];
                             NSLog(@" xy is:%@",xy);
                             image =[ [UIImageView alloc ] initWithImage:[xy objectAtIndex:0]];
                             NSLog(@"image is:%@",image);
                         }
                         failureBlock:^(NSError *error){ NSLog(@"test:Fail"); } ]; 
            } 
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];
    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        NSLog(@"hello");
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            NSLog(@"Number of assets in group :%d",[group numberOfAssets]);
            NSLog(@"asset group is:%@",assetGroups);
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
             usingBlock:assetGroupEnumerator
             failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM