为了账号安全,请及时绑定邮箱和手机立即绑定

在iPhone中显示从ALAsset检索到的URL图像

在iPhone中显示从ALAsset检索到的URL图像

呼啦一阵风 2019-07-08 17:32:32
在iPhone中显示从ALAsset检索到的URL图像我使用ALAssetFramework访问设备照片库中的文件。到目前为止,我能够访问缩略图并显示它。我想在图像视图中显示实际的图像,但是我想不出如何做到这一点。我尝试在ALAsset对象中使用URL字段,但没有成功。有人知道怎么做吗?下面是一些我能够访问缩略图并将其放在表格单元格中的代码:- (UITableViewCell *)tableView:(UITableView *)tableView           cellForRowAtIndexPath:(NSIndexPath *)indexPath {   static NSString *CellIdentifier = @"Cell";   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   if (cell == nil) {     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];   }   //here 'asset' represents the ALAsset object   asset = [assets objectAtIndex:indexPath.row];        //i am accessing the thumbnail here   [cell.imageView setImage:[UIImage imageWithCGImage:[asset thumbnail]]];   [cell.textLabel setText:[NSString stringWithFormat:@"Photo %d", indexPath.row+1]];   return cell;}
查看完整描述

3 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

对于某些人来说,一件有用的事情是同时包含图像定向和缩放元数据。您可以在结果块中这样做,如下所示:

ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
    ALAssetRepresentation *rep = [myasset defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    if (iref) 
    {
        UIImage *largeimage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]];
        [delegate hiresImageAvailable:large];
    }};

这个imageWIthCGImage在这种情况下呼叫有规模和orientation创建UIImage为了你。

[UIImage imageWithCGImage:iref scale:[rep scale] orientation:[rep orientation]];

需要注意的一个技巧是,如果你使用[rep fullScreenImage]而不是[rep fullResolutionImage]在iOS 5上,你会得到一张已经旋转的图像-不过它是iPhone屏幕的分辨率-也就是说,它的分辨率较低。


查看完整回答
反对 回复 2019-07-08
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

把Warren和OKnox的答案组合成一个简短的片段:

ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];[assetsLibrary assetForURL:self.selectedPhotos[i] resultBlock: ^(ALAsset *asset){
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    CGImageRef imageRef = [representation fullResolutionImage];
    if (imageRef) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        imageView.image = [UIImage imageWithCGImage:imageRef scale:representation.scale orientation:representation.orientation];
        // ...
    }} failureBlock: ^{
    // Handle failure.}];

我个人喜欢把我的failureBlocknil.


查看完整回答
反对 回复 2019-07-08
  • 3 回答
  • 0 关注
  • 521 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信