Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 只有数组中的最后一个图像存储才能在表视图中显示_Ios - Fatal编程技术网

Ios 只有数组中的最后一个图像存储才能在表视图中显示

Ios 只有数组中的最后一个图像存储才能在表视图中显示,ios,Ios,下面的代码将在数组中生成正确数量的数据,但显示数据将只取最后一个值并重复显示。 例如: 当我选择第一个图像时,第一个图像成功地显示在表视图中。 当我选择第二个图像时,数组将有2个数据,但问题是在表视图中,我将得到2个相同的图像(第二个选择的图像)。我的预期结果是,当选择第二个图像时,第一个图像仍将在那里,第二个显示在子序列行 - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"Collector in photoList %@",self.

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

当我选择第一个图像时,第一个图像成功地显示在表视图中。 当我选择第二个图像时,数组将有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;
}

已编辑完整代码

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


}
 #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
 - (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;
ImageModel.h

#import <Foundation/Foundation.h>

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

@interface ImageModel : NSObject

@property (nonatomic, strong) NSURL *imageUrl;

- (void)getImageWithCompletionHandler:(handler)completionBlock;

@end
}

控制器 控制器

GalleryImage只是一个UIImage。每次调用assetForURL:方法时,galleryImage都将被替换。很明显,在for循环完成后,galleryImage将包含最后一个图像。代替galleryImage,u可以使用NSMutableArray并为循环中的每次迭代将图像添加到数组中。这是可行的(请参阅上面我编辑的代码),但如果用户删除在表视图中添加的图像会有点麻烦,因为需要从不同的数组中逐个删除。有其他简单的方法来维持数组吗?最好考虑有一个模型。我已经编辑了下面添加模型的代码。希望它有帮助:)。嗨,诺伯特,你能解释更多的细节模型的方式,你建议或得到任何参考,因为我是完全新的与此模型。我尝试在谷歌搜索,但它给了我很多无关的模型给我。我已经更新了代码。你可以参考GalleryImage只是一个UIImage。每次调用assetForURL:方法时,galleryImage都将被替换。很明显,在for循环完成后,galleryImage将包含最后一个图像。代替galleryImage,u可以使用NSMutableArray并为循环中的每次迭代将图像添加到数组中。这是可行的(请参阅上面我编辑的代码),但如果用户删除在表视图中添加的图像会有点麻烦,因为需要从不同的数组中逐个删除。有其他简单的方法来维持数组吗?最好考虑有一个模型。我已经编辑了下面添加模型的代码。希望它有帮助:)。嗨,诺伯特,你能解释更多的细节模型的方式,你建议或得到任何参考,因为我是完全新的与此模型。我尝试在谷歌搜索,但它给了我很多无关的模型给我。我已经更新了代码。你可以参考我应该在那里的评论中输入的内容:-(void)viewDidLoad{[super viewDidLoad];self.imageModelObjects=//应该包含图像模型对象。}为了我的澄清,在controller.m的viewDidLoad中,我们是否将url传递给imageModel.m以加载图像?对于(self.collector中的NSURL*url){ImageModel*ImageModel=[[ImageModel alloc]init];ImageModel.url=url;[self.imageModelObjects addObject:ImageModel]}问题不清楚。我们的目标不是在数组中存储NSURL对象,而是存储我们自己的自定义对象,即ImageModel。每个ImageModel对象都应该知道它有什么url,并且还应该返回url的图像。我对ImageModel.h中使用的imageUrl感到困惑,其中declare as属性,imageUrl从何处获取数据?抱歉理解速度太慢。imageUrl是.h文件中的接口,意味着它应该由创建它的对象设置。在viewDidLoad中,如果您检查,我们将通过[[ImageModel alloc]init]创建ImageModel对象,在下一行中,我们将设置从收集器数组中获取的url。假设您的收集器数组具有NSURL对象。我应该在那里的注释中输入:-(void)viewDidLoad{[super viewDidLoad];self.imageModelObjects=//应该包含图像模型对象。}为了我的澄清,在controller.m的viewDidLoad中,是否将url传递给imageModel.m以加载图像?对于(self.collector中的NSURL*url){ImageModel*ImageModel=[[ImageModel alloc]init];ImageModel.url=url;[self.imageModelObjects addObject:ImageModel]}问题不清楚。我们的目标不是在数组中存储NSURL对象,而是存储我们自己的自定义对象,即ImageModel。每个ImageModel对象都应该知道它有什么url,并且还应该返回url的图像。我对ImageModel.h中使用的imageUrl感到困惑,其中declare as属性,imageUrl从何处获取数据?抱歉理解速度太慢。imageUrl是.h文件中的接口,意味着它应该由创建它的对象设置。在viewDidLoad中,如果您检查,我们将通过[[ImageModel alloc]init]创建ImageModel对象,在下一行中,我们将设置从收集器数组中获取的url。假设收集器阵列具有NSURL对象。
 - (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
#import <Foundation/Foundation.h>

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

@interface ImageModel : NSObject

@property (nonatomic, strong) NSURL *imageURL;

- (void)getImageWithCompletionHandler:(handler)completionBlock;

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