4

フォト ライブラリから画像を取得し、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");}];
4

2 に答える 2

3

このコードは、ここの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];
于 2012-07-07T11:31:59.827 に答える
3

私のコードは次のとおりです。

- (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");}];
于 2012-07-17T06:36:41.930 に答える