当用户从图像选择器中选择图像时,我以这种方式获取图像资产。
-(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
总是变成空的。这是为什么。请帮我。谢谢