需要在 UITableViewCell 中将 iPhone 画廊图片显示为缩略图 [英] Needs to display iPhone gallery images as thumbnails in UITableViewCell

查看:26
本文介绍了需要在 UITableViewCell 中将 iPhone 画廊图片显示为缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application,I am allowing user to select one or multiple images from gallery and as the user is selecting the images,I am storing all the url in an array

- (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"
)

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

Now my problem is that I don't know how to use these value and display them in my table view cell.

推荐答案

数组的每个元素中都有一个资产 URL.要从资产 URL 获取图像,从 ALAssetsLibrary 对象开始,调用 assetForURL:resultBlock:failureBlock: 以获取相应的资产(照片文件),然后在 resultBlock,拉出缩略图并在您的界面中使用它.

What you've got in each element of your array is an asset URL. To get from an asset URL to an image, start with the ALAssetsLibrary object, call assetForURL:resultBlock:failureBlock: to fetch the corresponding asset (the photo file), and, in the resultBlock, pull out the thumbnail and use it in your interface.

以下是从 ALAsset 获取可用图像的一些典型代码:

Here's some typical code for getting from an ALAsset to a usable image:

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: 耗时且异步操作;它不适合在表格视图数据源方法中调用(当你配置你的单元格时),它需要很快.在didFinishPickingMediaWithInfo:中,你为什么要向info询问它的UIImagePickerControllerReferenceURL?您不需要 URL 数组;你需要一组图像!info 正在为您提供图像(它的 UIImagePickerControllerOriginalImage);当它被提供时你为什么不接受它?您可以直接抓取图像,将其缩小,然后将缩小的图像粘贴到您的表数据模型中,以备使用.

However, you've made a very odd design choice here. assetForURL:resultBlock:failureBlock: is time-consuming and operates asynchronously; it is not suitable for calling in a table view data source method (when you configure your cells), which needs to be fast. In didFinishPickingMediaWithInfo:, why did you ask the info for its UIImagePickerControllerReferenceURL? You don't need an array of URLs; you need an array of images! The info was offering you the image right then (its UIImagePickerControllerOriginalImage); why didn't you take it when it was offered? You could have just grabbed the image, sized it down, and stuck the sized-down image into your table data model, ready for use.

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

(Also, it is silly and wasteful to call reloadData every time through the for loop. You should call it just once, after you've gathered all the images into your model.)

这篇关于需要在 UITableViewCell 中将 iPhone 画廊图片显示为缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆