Установить изображение в UIImageView с помощью UIImagePickerControllerReferenceURL - PullRequest
2 голосов
/ 01 декабря 2011

Можно ли установить изображение на UIImageView, используя UIImagePickerControllerReferenceURL. Традиционная модель использует [info objectForKey: @ "UIImagePickerControllerOriginalImage"];

I tried this:

theImageView.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [info objectForKey: UIImagePickerControllerReferenceURL]]];

Но изображение не установлено в UIImageView.

Пожалуйста, помогите.

Ответы [ 2 ]

5 голосов
/ 02 декабря 2011
NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library assetForURL:referenceURL resultBlock:^(ALAsset *asset)
         {
           UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
             theImageView.image = copyOfOriginalImage;
         }
                failureBlock:^(NSError *error)
         {
             // error handling
         }];
        [library release];

Этот код добился цели.:)

1 голос
/ 01 декабря 2011

Вместо того, чтобы встраивать четыре или пять функций в одну строку, вам нужно больше обрабатывать ошибки в своем коде.

Рассмотрим это вместо:

NSURL * referenceURL = [info objectForKey: UIImagePickerControllerReferenceURL];
if(referenceURL == NULL)
{
    NSLog( @"reference URL is null");
} else {
    NSData * imageData = [NSData dataWithContentsOfURL: referenceURL];
    if(imageData == NULL)
    {
        NSLog( @"no image data found for URL %@", [referenceURL absoluteString] );
    } else {
        UIImage * theActualImage = [UIImage imageWithData: imageData];
        if(theActualImage == NULL)
        {
            NSLog( @"the data at URL %@ is not an image", [referenceURL absoluteString] );
        } else {
            if(theImageView == NULL)
            {
                NSLog( @"I forgot to set theImageView" );
            } else {
                theImageView.image = theActualImage;
            }
        }
    }
}

Надеюсь, эта информация поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...