图像选择器iOS

时间:2015-03-31 13:19:00

标签: ios uiimagepickercontroller

在我的图片选择器中,我使用了以下代码

- (void)imageAtIndex:(NSInteger)index completionHandler:(void(^)(UIImage *image))completionHandler
{
  NSDictionary *image = self.images[index];
  [self.library assetForURL:image[@"URL"] resultBlock:^(ALAsset *asset) {
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    UIImage *returnImage = [UIImage imageWithCGImage:[representation fullResolutionImage]
                                               scale:[representation scale]
                                         orientation:(int)[representation orientation]];
    completionHandler(returnImage);
} failureBlock:^(NSError *error) {
    NSLog(@"Error loading asset");
}];
}

我使用以下代码选择图像

[self.cameraRoll imageAtIndex:indexPath.row-1 completionHandler:^(UIImage *image) {
        myImage=image;
    }];

当我第一次选择时,当我第二次选择相同的图像时,它将值设为“nil”,它正在正确地拾取图像。

如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我希望这会对某人有所帮助,所以发布答案。我们必须等到库被加载,因为我正在使用while循环...这样它才能正常工作

- (void)imageAtIndex:(NSInteger)index completionHandler:(void(^)(UIImage *image1))completionHandler
{
__block BOOL isFinished = NO;

NSDictionary *image = self.images[index];


[self.library assetForURL:image[@"URL"] resultBlock:^(ALAsset *asset) {


    ALAssetRepresentation *representation = [asset defaultRepresentation];
    UIImage *returnImage = [UIImage imageWithCGImage:[representation fullResolutionImage]
                                               scale:[representation scale]
                                         orientation:(int)[representation orientation]];

   isFinished = YES;

    completionHandler(returnImage);
} failureBlock:^(NSError *error) {
    NSLog(@"Error: Cannot load asset - %@", [error localizedDescription]);
    isFinished = YES;
}];
while (!isFinished) {
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
}
}