ios - 需要在 UITableViewCell 中将 iPhone 图库图像显示为缩略图

标签 ios uitableview uiimagepickercontroller

在我的应用程序中,我允许用户从图库中选择一个或多个图像,当用户选择图像时,我将所有 url 存储在一个数组中

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];

    for (NSDictionary *dict in info) {
    url = dict[@"UIImagePickerControllerReferenceURL"];
        [self.images addObject:url];
        NSLog(@"array value:%@",self.images);
        [self.myPDTable reloadData];
   }
}

我得到的输出是

array value:(
    "assets-library://asset/asset.JPG?id=74236046-12DA-4E75-9038-A3092D31005B&ext=JPG",
    "assets-library://asset/asset.JPG?id=98FC160A-60E1-4F29-80C8-1AED036F1140&ext=JPG",
    "assets-library://asset/asset.JPG?id=4D18448E-9796-47AE-A534-FB3E64518B89&ext=JPG",
    "assets-library://asset/asset.JPG?id=38099BA1-4988-46CB-ADE9-E1F3F896147A&ext=JPG",
    "assets-library://asset/asset.JPG?id=86556040-16C6-47BB-8CBA-F5164771EC9F&ext=JPG"
)

现在我的问题是我不知道如何使用这些值并将它们显示在我的表格 View 单元格中。

最佳答案

您在数组的每个元素中得到的是一个 Assets URL。要从 Assets URL 获取图像,从 ALAssetsLibrary 对象开始,调用 assetForURL:resultBlock:failureBlock: 以获取相应的 Assets (照片文件),并且在 resultBlock,拉出缩略图并在您的界面中使用它。

这是从 ALAsset 到可用图像的一些典型代码:

ALAsset* photo = // let's assume you've obtain an asset
CGImageRef im = photo.aspectRatioThumbnail;
UIImage* im = [UIImage imageWithCGImage:im scale:0
                            orientation:UIImageOrientationUp];

但是,您在这里做出了一个非常奇怪的设计选择。 assetForURL:resultBlock:failureBlock: 耗时,异步操作;它不适合调用需要快速的 TableView 数据源方法(当您配置单元格时)。在 didFinishPickingMediaWithInfo: 中,为什么要向 info 询问它的 UIImagePickerControllerReferenceURL?您不需要一组 URL;你需要一组图像! info 正在为您提供图像(它的 UIImagePickerControllerOriginalImage);提供时你为什么不接受?您可以直接抓取图像,将其缩小,然后将缩小后的图像粘贴到表格数据模型中,以备使用。

(此外,每次通过 for 循环调用 reloadData 是愚蠢和浪费的。你应该只调用一次,在你收集了所有图像之后进入你的模型。)

关于ios - 需要在 UITableViewCell 中将 iPhone 图库图像显示为缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22966545/

相关文章:

ios - nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewHeaderFooterView 实例

用于检查铁路 PNR 状态和列车查询的 iPhone 应用程序

ios - 为什么在动画 block 中显示 View 时 UIWebView scalespagetofit 不起作用?

ios - 如何减去日期成分?

ios - 在 View 中包含 UIImagePickerController

ios - UIImagePickerController 损坏图像

iphone - UIImagePickerController 未在 iPhone SDK 的 viewDidLoad 中加载

iOS - 如何在不同的 UITableViewCell 中重新使用 UIView

ios: uitableview - 如何删除第一部分

ios - 方向更改后 UITableView 布局中的错误