- [UIImage length]:使用带图像的NSMutableArray将无法识别的选择器发送到实例错误

时间:2013-04-17 13:03:46

标签: ios uiimage nsmutablearray uiimagepickercontroller uicollectionviewcell

我有一个具有UIViewControllerUICollectionViewController的故事板应用。在视图控制器中,用户从iPhone的照片库中选择多张照片(由于iOS中没有用于多选的API,我使用ELCImagePickerController来实现此目的。并且它会转移到集合视图控制器,其中所选照片应以小图片视图显示。

图像库显示,我可以选择多张照片。但当它转移到集合视图控制器时,它会在集合视图的cellForItemAtIndexPath事件中抛出 - [UIImage length]:无法识别的选择器发送到实例错误。

以下是我到目前为止的代码。

ViewController.h

#import <UIKit/UIKit.h>
#import "ELCImagePickerController.h"
#import "ELCAlbumPickerController.h"
#import "ELCAssetTablePicker.h"
#import "GalleryViewController.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) NSMutableArray *cameraImages;

- (IBAction)chooseImages:(id)sender;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)chooseImages:(id)sender
{
    UIActionSheet *photoSourcePicker = [[UIActionSheet alloc] initWithTitle:nil
                                                                   delegate:self
                                                          cancelButtonTitle:@"Cancel"
                                                     destructiveButtonTitle:nil
                                                          otherButtonTitles:@"Take Photo", @"Choose from Library", nil, nil];
    [photoSourcePicker showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:nil bundle:nil];
                ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
                albumController.parent = elcPicker;
                elcPicker.delegate = self;

                if ([self.view respondsToSelector:@selector(presentViewController:animated:completion:)]){
                    [self presentViewController:elcPicker animated:YES completion:nil];
                } else {
                    [self presentViewController:elcPicker animated:YES completion:nil];
                }
            }
            else {
                UIAlertView *alert;
                alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                   message:@"This device doesn't have a camera"
                                                  delegate:self
                                         cancelButtonTitle:@"Ok"
                                         otherButtonTitles:nil, nil];
                [alert show];
            }
            break;

        case 1:
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] initWithNibName:nil bundle:nil];
                ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
                albumController.parent = elcPicker;
                elcPicker.delegate = self;

                if ([self.view respondsToSelector:@selector(presentViewController:animated:completion:)]){
                    [self presentViewController:elcPicker animated:YES completion:nil];
                } else {
                    [self presentViewController:elcPicker animated:YES completion:nil];
                }
            }
            else {
                UIAlertView *alert;
                alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                   message:@"This device doesn't support photo libraries"
                                                  delegate:self
                                         cancelButtonTitle:@"Ok"
                                         otherButtonTitles:nil, nil];
                [alert show];
            }
            break;
    }
}

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];

    self.cameraImages = [[NSMutableArray alloc] initWithCapacity:info.count];


    for (NSDictionary *camImage in info) {
        UIImage *image = [camImage objectForKey:UIImagePickerControllerOriginalImage];
        [self.cameraImages addObject:image];
    }
    /*
     for (UIImage *image in info) {
     [self.attachImages addObject:image];
     }
     */

    NSLog(@"number of images = %d", self.cameraImages.count);
    if (self.cameraImages.count > 0) {
        [self performSegueWithIdentifier:@"toGallery" sender:nil];
    }
}


- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"toGallery"]) {
        GalleryViewController *galleryVC = [segue destinationViewController];
        galleryVC.selectedImages = self.cameraImages;
    }
}

@end

GalleryViewController.h

#import <UIKit/UIKit.h>
#import "ELCImagePickerController.h"
#import "ELCAlbumPickerController.h"
#import "ELCAssetTablePicker.h"
#import "ImageCell.h"

@interface GalleryViewController : UICollectionViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) NSMutableArray *selectedImages;

@end

GalleryViewController.m

#import "GalleryViewController.h"

@interface GalleryViewController ()

@end

@implementation GalleryViewController


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.selectedImages.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imgCell" forIndexPath:indexPath];
    UIImage *image;
    int row = indexPath.row;

    image = [UIImage imageNamed:self.selectedImages[row]]; //This is where it throws the error
    cell.imageView.image = image;

    return cell;
}

@end

为了进一步证明这个问题,我把一个可以从here下载的演示项目打包了。

我知道这个问题已经在很久以前被问过了。在这里发布我的问题之前,我尝试了所有这些但是没有用。

如果有人能告诉我如何摆脱这个错误,我会很感激。

谢谢。

2 个答案:

答案 0 :(得分:13)

嘿我理解你的问题你已经有了一系列图像,为什么再次使用imageNamed:constructor。

image = [UIImage imageNamed:self.selectedImages[row]];
cell.imageView.image = image;
//This throws a exception because, you have UIImage objects in your array and here imageNamed: takes NSString as an argument , so you are trying to pass a UIImage object instead of a NSString object

直接从数组中取出图像并按如下方式分配:

cell.imageView.image = (UIImage*) [self.selectedImages objectAtIndex:row];

可能不需要UIImage *。

答案 1 :(得分:1)

self.selectedImages[row]应为NSString。它似乎是UIImage而不是NSString。它试图在UIImage实例上调用长度方法。