在Table视图中使用AssetLibrary显示图像

时间:2013-06-26 08:47:06

标签: ios

目前我有2个视图控制器,我从视图控制器中选择一张照片,A并将图像网址传递给第二个视图控制器,B并使用网址检索从A中选择的图像。但是问题是可以检索图像在viewDidLoad但无法在表viewcellForRowAtIndexPath检索。我不确定有人帮助我的根本原因是什么?

// A.M

(void)imagePickerController:(UIImagePickerController *)picker   didFinishPickingMediaWithInfo:(NSDictionary *)info {

// Initialize  View Controller B
B *b = [[B alloc]initWithNibName:@"PhotosListViewController" bundle:nil];

// get the ref url
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
photoListViewController.test = imageURL;
NSLog(@"%@",imageURL);
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:photoListViewController animated:YES];
}


- (IBAction)organiseAttachement:(id)sender {

// Initialize  View Controller
B *b = [[PhotosListViewController alloc]initWithNibName:@"B" bundle:nil];
[self.navigationController pushViewController:b animated:YES];
}

// B.h

#import <UIKit/UIKit.h>

@interface PhotosListViewController : UITableViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, strong) NSURL *test;

@end

// B.m

- (void)viewDidLoad
{
    [super viewDidLoad];

   NSLog(@"Image is THIS --- %@",test);

// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
    CGImageRef iref = [imageRep fullResolutionImage];
    if (iref) {
        self.galleryImage = [UIImage imageWithCGImage:iref];
    }
    [self.tableView reloadData];
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);
    NSLog(@"[imageRep image] : %@", galleryImage);
};

// get the asset library and fetch the asset based on the ref url (pass in block above)

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:test resultBlock:resultblock failureBlock:nil]
}

(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];

}

   cell.imageView.image = self.galleryImage;
   return cell;
}

1 个答案:

答案 0 :(得分:0)

替换

// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
    CGImageRef iref = [imageRep fullResolutionImage];
    if (iref) {
        self.galleryImage = [UIImage imageWithCGImage:iref];
    }
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);
    NSLog(@"[imageRep image] : %@", galleryImage);
};

// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
    CGImageRef iref = [imageRep fullResolutionImage];
    if (iref) {
        self.galleryImage = [UIImage imageWithCGImage:iref];
    }
    [self.tableView reloadData]; //Reload the tableView.
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);
    NSLog(@"[imageRep image] : %@", galleryImage);
};

编辑:

A.M

@interface A ()

@property(nonatomic,strong) B *b;

@end

@implementation b

-(void)imagePickerController:(UIImagePickerController *)picker   didFinishPickingMediaWithInfo:(NSDictionary *)info {

if(self.b)
{
   // Initialize  View Controller B
   self.b = [[B alloc]initWithNibName:@"PhotosListViewController" bundle:nil];
}

// get the ref url
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
self.b.test = imageURL;
NSLog(@"%@",imageURL);
[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:self.b animated:YES];
}


- (IBAction)organiseAttachement:(id)sender {

if(self.b == nil)
{
   // Initialize  View Controller
   self.b = [[PhotosListViewController alloc]initWithNibName:@"B" bundle:nil];
}
[self.navigationController pushViewController:self.b animated:YES];
}

@end