2013-06-28 59 views
1

我的代码如下,将产生正确数量的数组,但显示数据只会取最后一个值并重复显示。 例如:只有最后图像存储在阵列中获取显示在表视图

当我选择第一张图片时,第一张图片成功显示在表格视图中。 当我选择第二个图像时,数组将有2个数据,但问题是在表视图中,我会得到2个相同的图像(第二个选定的图像)。我的预期结果将是当选择第二个图像时,第一个图像仍然在那里,第二个显示在子序列行。

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

}

EDITED!

- (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 EDITED代码!!

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

}

EDITED全码!!

RootViewController.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]; 


} 

ImageModel.h

#import <Foundation/Foundation.h> 

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

@interface ImageModel : NSObject 

@property (nonatomic, strong) NSURL *imageUrl; 

- (void)getImageWithCompletionHandler:(handler)completionBlock; 

@end 

ImageModel.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; 

}

+0

GalleryImage只是一个UIImage。每次你调用assetForURL:方法,galleryImage都会被替换。在for循环完成之后如此明显,您的galleryImage将包含最后一幅图像。您可以使用NSMutableArray代替galleryImage,并将图像添加到循环中的每个迭代的数组中。 – iCoder

+0

这是工作(请参阅我上面编辑的代码),但是如果用户删除添加在表视图中的图像是有点麻烦,因为需要从不同的数组中逐个删除。它有其他简单的方法来维护数组吗? – yukiko

+0

最好考虑建立一个模型。我编辑了下面添加模型的代码。希望能帮助到你 :) 。 – iCoder

回答

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 

编辑:

ImageModel.h
#import <Foundation/Foundation.h> 

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

@interface ImageModel : NSObject 

@property (nonatomic, strong) NSURL *imageURL; 

- (void)getImageWithCompletionHandler:(handler)completionBlock; 

@end 

ImageModel.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]; 
} 

Controller.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; 
} 
+0

我应该把什么放在评论有: - (void)viewDidLoad { [super viewDidLoad]; self.imageModelObjects = //应该包含图像模型对象。 } – yukiko

+0

为了说明问题,在controller.m的viewDidLoad中,我们将url传递给imageModel.m以加载图像? for(NSURL * url in self.collector) { ImageModel * imageModel = [[ImageModel alloc] init]; imageModel.url = url; [self.imageModelObjects addObject:imageModel] } – yukiko

+0

问题不清楚。我们的目标不是将NSURL对象存储在数组中,而是存储我们自己的自定义对象,即ImageModel。每个ImageModel对象应该知道它有哪个url,并且应该返回url的图像。 – iCoder

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