2

私のiPhoneアプリケーションには、ユーザーフォームギャラリーによって選択された画像パスを保存するための配列があります. ALAssetsLibrary を使用して、配列内のすべての画像パスを uiimage に変換します。どうすればこのアクションを実行できますか? ループを使用してみましたが、できません。助けてくれてありがとう。

ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:[filePath objectAtIndex:0] resultBlock:^(ALAsset *asset) {
    UIImage *image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
    [fileImage addObject:image];
} failureBlock:^(NSError *error) {

}];
4

1 に答える 1

6

以下のコードを使用してください

typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);    
                       
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
                       
 ALAssetRepresentation *rep = [myasset defaultRepresentation];
 CGImageRef iref = [rep fullResolutionImage];
            
 if (iref){

         dispatch_async(dispatch_get_main_queue(), ^{
             UIImage *myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
             [fileImage addObject:myImage];
             //binding ur UI elements in main queue for fast execution
             //self.imageView.image = myImage;
         });

                                
         }      
};      
                       
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){
                       
     //failed to get image.
};                          
                       
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:[filePath objectAtIndex:0] resultBlock:resultblock failureBlock:failureblock];

注: [filePath objectAtIndex:0]NSUrlがオブジェクトになることを確認してください。そうでない場合は、に変換してくださいNSUrl

例:

ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];

NSURL myAssetUrl = [NSURL URLWithString:[filePath objectAtIndex:0]];

assetslibrary assetForURL:myAssetUrl resultBlock:resultblock failureBlock:failureblock];
于 2012-07-30T14:04:02.090 に答える