4

我有一个故事板应用程序,它有一个UIViewController和一个UICollectionViewController. 在视图控制器中,用户从 iPhone 的照片库中选择多张照片(由于 iOS 中没有用于多选的 API,我使用ELCImagePickerController来实现这一点)。并且它与collection view controller 相连,在该控制器中,所选照片应显示在小图像视图中。

图像库出现,我可以选择多张照片。但是当它转到集合视图控制器时,它会在集合视图的事件中抛出-[UIImage length]: unrecognized selector sent to instance错误。cellForItemAtIndexPath

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

视图控制器.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

视图控制器.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

为了进一步演示这个问题,我制作了一个演示项目,您可以从这里下载。

我知道在此之前已经多次问过这个问题。在在这里发布我的问题之前,我尝试了所有但无济于事。

如果有人能告诉我如何摆脱这个错误,我将不胜感激。

谢谢你。

4

2 回答 2

13

嘿,我理解你的问题,你已经有一个图像数组,为什么再次使用 imageNamed: 构造函数。

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 * 可能不需要。

于 2013-04-17T13:19:28.807 回答
1

self.selectedImages[row]应该是一个NSString。似乎它是 aUIImage而不是NSString. 它试图在UIImage实例上调用 length 方法。

于 2013-04-17T13:17:52.820 回答