gpt4 book ai didi

ios - 仅存储在数组中的最后一个图像在表格 View 中显示

转载 作者:行者123 更新时间:2023-11-29 13:11:04 25 4
gpt4 key购买 nike

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

当我选择第一张图片时,第一张图片成功显示在表格 View 中。当我选择第二个图像时,数组将有 2 个数据但问题是在 TableView 中我将得到 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;

已编辑!

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

已编辑完整代码!!

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;

最佳答案

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

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

关于ios - 仅存储在数组中的最后一个图像在表格 View 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17356340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com