如何在UITableview中显示图像的资产URL?

时间:2014-04-19 06:23:49

标签: ios objective-c cocoa-touch uitableview

您好我有一个包含以下资产网址的数组

 (
"assets-library://asset/asset.JPG?id=265B828F-0C5F-41DE-8284-89188A7515B1&ext=JPG",
"assets-library://asset/asset.JPG?id=09212CE0-1411-492E-AFCA-0024459F111C&ext=JPG"

我需要在UITableview中将此url显示为图像。我可以显示这个吗?我的UITableview代码

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
    }
    NSURL* aURL = [NSURL URLWithString:@"URL here"];

    [library assetForURL:aURL resultBlock:^(ALAsset *asset)
     {
         UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];

         cell.imageView.image = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] ];
     }
            failureBlock:^(NSError *error)
     {
         // error handling
         NSLog(@"failure-----");
     }];
  //  cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[img objectAtIndex:0]]]];

    return cell;
}

1 个答案:

答案 0 :(得分:0)

答案是

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
    }
    NSURL* aURL = [NSURL URLWithString:@"URL here"];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep = [myasset defaultRepresentation];
        CGImageRef iref = [rep fullResolutionImage];
        if (iref) {
            UIImage *largeimage = [UIImage imageWithCGImage:iref];
            cell.imageView.image = largeimage;
        }
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can't get image - %@",[myerror localizedDescription]);
    };

    NSURL *asseturl = [NSURL URLWithString:[img objectAtIndex:indexPath.row]];
     library = [[ALAssetsLibrary alloc] init] ;
     [library assetForURL:asseturl
                    resultBlock:resultblock
                   failureBlock:failureblock];
    //  cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[img objectAtIndex:0]]]];
    cell.textLabel.text = [latitude objectAtIndex:indexPath.row];
    cell.detailTextLabel.text=[intensity objectAtIndex:indexPath.row];
    return cell;
}