2013-06-28 64 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; 
}