【问题标题】:Set Image on UIImageView using UIImagePickerControllerReferenceURL使用 UIImagePickerControllerReferenceURL 在 UIImageView 上设置图像
【发布时间】:2012-01-10 17:29:09
【问题描述】:

是否可以使用 UIImageView 设置图像 UIImagePickerControllerReferenceURL 。传统模式是使用 [info objectForKey:@"UIImagePickerControllerOriginalImage"];

I tried this:

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

但 UIImageView 上没有设置图像。

请帮忙。

【问题讨论】:

    标签: iphone uiimageview


    【解决方案1】:

    您需要在代码中进行更多的错误处理,而不是在一行中嵌入四个或五个函数。

    请考虑这个:

    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;
                }
            }
        }
    }
    

    希望这些信息对您有所帮助。

    【讨论】:

    • 我在控制台中得到了这个没有找到 URL assets-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPG 的图像数据但我从照片库。似乎是什么问题?
    • 很高兴您能够找出问题所在。现在看起来您需要弄清楚如何将该引用 URL 转换为您可以实际加载到 NSData 对象中的内容。也许info NSDictionary 中的另一个键可以帮助您? :-)
    • 但是 URL 似乎是一个有效的 :( 那为什么没有图像数据?
    • 您必须使用ALAssetsLibrary 访问该类型的图像数据。查找涉及ALAssetsLibraryAssetForURLResultBlock的示例代码。
    • 对不起@Michael Dautermann 在网上没有找到任何有用的东西。我是 iPhone 新手。你能给我一些建议吗?
    【解决方案2】:
    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];
    

    这段代码成功了。 :)

    【讨论】:

    • 如果有人能把它转换成 Swift 2.0 会很有帮助
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2020-04-15
    • 1970-01-01
    相关资源
    最近更新 更多