Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

ios - Display Image Using AssetLibrary at Table view

Currently i have 2 view Controllers, i select a photo from view controller,A and pass the image url to second view controller,B and use the url to retrieve the image selected from A.But problem is the image can be retrieved at viewDidLoad but cannot retrieve at table viewcellForRowAtIndexPath. I not sure what is the root cause can somebody help me?

//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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Replace

// 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);
};

with

// 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);
};

EDITED:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...