1

当用户从图像选择器中选择图像时,我以这种方式获取图像资产。

-(NSMutableData *)GetImageAsset :(int)index
   {
dm=[DataManager sharedManager];
NSURL *url=[dm.imgAssetsArr objectAtIndex:index];
__block NSMutableData *data;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:url resultBlock:^(ALAsset *asset)
 {


         if (asset) {
             ALAssetRepresentation *representation = [asset defaultRepresentation];
             data = [NSMutableData data];
             NSError *error;
             long long bufferOffset = 0ll;
             NSInteger bufferSize = 10000;
             long long bytesRemaining = [representation size];
             uint8_t buffer[bufferSize];
             while (bytesRemaining > 0) {
                 NSUInteger bytesRead = [representation getBytes:buffer fromOffset:bufferOffset length:bufferSize error:&error];
                 if (bytesRead == 0) {
                     NSLog(@"error reading asset representation: %@", error);
                     return;
                 }
                 bytesRemaining -= bytesRead;
                 bufferOffset   += bytesRead;
                 [data appendBytes:buffer length:bytesRead];
         }



 }

 }
        failureBlock:^(NSError *error)
          {

               NSLog(@"assetForURL error = %@", error);

          }];


return data;
}

但这永远不会进入街区。只需跳过该块并转到return语句。所以我data总是变成空的。这是为什么。请帮我。谢谢

4

1 回答 1

0

如该方法的描述中所述:

讨论

此方法是异步的。

这意味着您不能以您编码的方式返回数据。取而代之的是,一旦检索到数据,就让块调用其他代码。

于 2015-12-14T11:18:34.913 回答