0

imagePickerControl で選択した画像を取得し、提供された URL を使用して開き、imageview に表示しようとしています。UIImagePickerControllerOriginalImage を使用して画像を設定すると正常に動作しますが、画像をデータベースに保存するのではなく、URL を保存したいだけです。これをコードにさらに拡張する前に、それが機能することを確認したかったのです。以下は、選択された画像を PickerController から返し、画像を表示しようとするコードです。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    self.imageURL=[info objectForKey:UIImagePickerControllerReferenceURL];
    CCLog(@"Image =%@",[info objectForKey:UIImagePickerControllerReferenceURL]);
//    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

    switch(status){
        case ALAuthorizationStatusDenied: {
            CCLog(@"not authorized");
            break;
        }
        case ALAuthorizationStatusRestricted: {
            CCLog(@"Restricted");
            break;
        }
        case ALAuthorizationStatusNotDetermined: {
            CCLog(@"Undetermined");
            break;
        }
        case ALAuthorizationStatusAuthorized: {
            CCLog(@"Authorized");
            CCLog(@"self.imageURL=%@",self.imageURL);
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            __block UIImage *returnValue = nil;
            [library assetForURL:self.imageURL resultBlock:^(ALAsset *asset) {
                returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
                } failureBlock:^(NSError *error) {
                    NSLog(@"error : %@", error);
            }];
            [self.imageDisplay setImage:returnValue];
            [self.imageDisplay setNeedsDisplay];
            break;
        }
        default: {
            CCLog(@"Unknown hit default");
            break;
        }

    }

    // You have the image. You can use this to present the image in the next view like you require in `#3`.

}

これを実行すると、トレースするとreturnValueがnilになり、画像が表示されません。https://stackoverflow.com/a/13276649/3723298からコードを取得しました

ありがとう

4

1 に答える 1

0

assetForURL:resultBlock:failureBlock:非同期です。次のようなものが必要です。

    case ALAuthorizationStatusAuthorized: {
        CCLog(@"Authorized");
        CCLog(@"self.imageURL=%@",self.imageURL);
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library assetForURL:self.imageURL resultBlock:^(ALAsset *asset) {
            UIImage *returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.imageDisplay setImage:returnValue];
                [self.imageDisplay setNeedsDisplay];
            });
        } failureBlock:^(NSError *error) {
            NSLog(@"error : %@", error);
        }];
        break;
    }

ところで - これのポイントは何ですか? 画像ピッカーのデリゲート メソッドから、より直接的に画像を取得できます。アセット ライブラリから画像を再度取得する必要はありません。

ただ行う:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.imageDisplay.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [self dismissViewControllerAnimated:YES completion:nil];
}
于 2014-07-27T03:42:02.197 に答える