1

我下面的代码将在数组中生成正确数量的数据,但显示数据只会采用最后一个值并重复显示。例如:

当我选择第一张图像时,第一张图像成功显示在表格视图中。当我选择第二个图像时,数组将有 2 个数据,但问题是在表格视图中我将获得 2 个相同的图像(第二个选择的图像)。My expected result will be when selected the second image the first image will still be there and the second display at the subsequence row.

- (void)viewDidLoad
{
 [super viewDidLoad];

 NSLog(@"Collector in photoList %@",self.collector);

for (int i = 0; i < collector.count; i++) {
// 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) {
        galleryImage = [UIImage imageWithCGImage:iref];
        [self.tableView reloadData];
    }
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);

};

NSLog(@"Collector %@",self.collector);

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

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc]init];
[assetslibrary assetForURL:[collector objectAtIndex:i] 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 = galleryImage;
NSLog(@"Gallery image is %@",self.galleryImage);
    return cell;

}

已编辑!

- (void)viewDidLoad
{
 [super viewDidLoad];

 NSLog(@"Collector in photoList %@",self.collector);

for (int i = 0; i < collector.count; i++) {
// 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) {
        galleryImage = [UIImage imageWithCGImage:iref];

        //Added mutable array for galleryImage
        [photoCollector addObject:galleryImage];

        [self.tableView reloadData];
   }
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);

};

NSLog(@"Collector %@",self.collector);

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

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc]init];
[assetslibrary assetForURL:[collector objectAtIndex:i] 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];

}

 //Display image 
if(photoCollector.count != 0)
{
    cell.imageView.image = [self.photoCollector objectAtIndex:indexPath.row];
}

NSLog(@"This is in cellForRowAtIndexPath");
NSLog(@"Gallery image is %@",self.galleryImage);

// Configure the cell...
return cell;

}

在选择器 didFinishPickingMediaWithInfo 编辑代码!

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

// Initialize  View Controller
PhotosListViewController *photoListViewController = [[PhotosListViewController alloc]initWithNibName:@"PhotosListViewController" bundle:nil];
ImageModel *imgModel = [[ImageModel alloc]init];

// get the ref url
imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];

//set the imageUrl to the imageModel url in property ?
imgModel.url = imageURL;

[self.collector addObject:imageURL];
photoListViewController.urlCollector = self.collector;
NSLog(@"Collector in root %@",self.collector);

[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:photoListViewController animated:YES];

}

编辑完整代码!!

根视图控制器.m

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

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

// get the ref url
imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];

[self.collector addObject:imageURL];
photoListViewController.urlCollector = self.collector;

NSLog(@"Collector in root %@",self.collector);

[picker dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:photoListViewController animated:YES];


}

图像模型.h

#import <Foundation/Foundation.h>

typedef void(^handler)(UIImage *image);

@interface ImageModel : NSObject

@property (nonatomic, strong) NSURL *imageUrl;

- (void)getImageWithCompletionHandler:(handler)completionBlock;

@end

图像模型.m

 #import "ImageModel.h"
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <AssetsLibrary/AssetsLibrary.h>

 @implementation ImageModel
 @synthesize imageUrl;

 - (void)getImageWithCompletionHandler:(handler)completionBlock
{
   ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
  {
    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
    CGImageRef iref = [imageRep fullResolutionImage];
    if (iref) {
        UIImage *image = [UIImage imageWithCGImage:iref];
        completionBlock(image);
    }

};

ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc]init];
[assetslibrary assetForURL:self.imageUrl resultBlock:resultblock failureBlock:nil];
}
@end

PhotoListViewController.m

 - (void)viewDidLoad
{

    [super viewDidLoad];
    test1 = [[UIImage alloc]init];
    self.imageModelObjects = [NSMutableArray array];
    for(NSURL *url in self.urlCollector)
  {
     ImageModel *imageModel = [[ImageModel alloc] init];
     imageModel.imageUrl = url;
     [self.imageModelObjects addObject:imageModel];
  }
}

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

      }    

ImageModel *model = [self.imageModelObjects objectAtIndex:indexPath.row];

[model getImageWithCompletionHandler:^(UIImage *image) {
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = image;
    });

}];


return cell;

}

4

2 回答 2

1
@interface ViewController () <UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *images;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.images = [[NSMutableArray alloc] init];

    NSLog(@"Collector in photoList %@",self.collector);

    for (int i = 0; i < collector.count; i++) {
        // 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.images addObject:[UIImage imageWithCGImage:iref]];
                [self.tableView reloadData];
            }
            NSLog(@"[imageRep filename] : %@", [imageRep filename]);

        };

        NSLog(@"Collector %@",self.collector);

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

        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc]init];
        [assetslibrary assetForURL:[collector objectAtIndex:i] resultBlock:resultblock failureBlock:nil];

    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.images.count;
}

- (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.images[indexPath.row];
    return cell;
}

@end

编辑:

图像模型.h

#import <Foundation/Foundation.h>

typedef void(^handler)(UIImage *image);

@interface ImageModel : NSObject

@property (nonatomic, strong) NSURL *imageURL;

- (void)getImageWithCompletionHandler:(handler)completionBlock;

@end

图像模型.m

#import "ImageModel.h"

@implementation ImageModel

- (void)getImageWithCompletionHandler:(handler)completionBlock
{
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
    {
        ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
        CGImageRef iref = [imageRep fullResolutionImage];
        if (iref) {
            UIImage *image = [UIImage imageWithCGImage:iref];
            completionBlock(image);
        }

    };

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

控制器.m

#import "ViewController.h"
#import "ImageModel.h"

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *imageModelObjects;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.imageModelObjects = [NSMutableArray array];
    for(NSURL *url in self.collector)
    {
         ImageModel *imageModel = [[ImageModel alloc] init];
         imageModel.url = url;
         [self.imageModelObjects addObject:imageModel]
     }

      //You can discard the collecter. IF u want the url, u can get from the self.imageModelObjects.
      self.collector = 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];

    }

    ImageModel *model = [self.imageModelObjects objectAtIndex:indexPath.row];

    [model getImageWithCompletionHandler:^(UIImage *image) {
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.imageView.image = image;
        });
    }];


    // Configure the cell...
    return cell;
}
于 2013-06-28T06:01:37.570 回答
0
if (iref)
{
    galleryImage = [UIImage imageWithCGImage:iref];

    //Added mutable array for galleryImage
    [photoCollector addObject:galleryImage];
    [photoCollector retain];


    //[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        cell.textLabel.text = @"Hello";
        cell.imageView.image = [self.photoCollector objectAtIndex:indexPath.row];

    }
    // Configure the cell.
    return cell;
}
于 2013-06-28T06:40:31.910 回答