【问题标题】:Image picker iOS图像选择器 iOS
【发布时间】:2015-03-31 13:18:59
【问题描述】:

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

- (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”,它正确地选择了图像。

如何解决这个问题。

【问题讨论】:

    标签: ios uiimagepickercontroller


    【解决方案1】:

    我希望这对某人有帮助,所以发布答案。我们必须等到库加载完毕,因为我正在使用 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]];
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多