-[UIImage Länge]: unrecognized selector geschickt Instanz-Fehler mit einem NSMutableArray mit Bildern
Habe ich ein storyboard app, die hat ein UIViewController
und ein UICollectionViewController
. In der view-controller, der Benutzer wählt mehrere Fotos aus der iPhone Foto-Bibliothek (Da es keine API für multi-select in iOS, die ich verwendet, ELCImagePickerController um dies zu erreichen). Und es leitet über zu der collection view controller, in dem die ausgewählten Fotos gezeigt werden soll, im kleinen Bild-Ansichten.
Bild-Bibliothek zeigt, und ich bin in der Lage, mehrere Fotos auswählen. Aber wenn es leitet über zu der collection view controller, wirft Sie das -[UIImage Länge]: unrecognized selector geschickt Instanz Fehler in der Ansicht Sammlung das cellForItemAtIndexPath
Veranstaltung.
Unten ist der code, den ich bisher haben.
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
Weiter das Problem veranschaulichen, hab ich schlug zusammen, eine demo-Projekt, das Sie herunterladen können hier.
Ich weiß, so Frage gestellt wurde viele Zeit, bevor hier auf SO. Ich habe alle ausprobiert, aber ohne Erfolg vor der Veröffentlichung meine Frage hier.
Ich würde schätzen, wenn mir jemand sagen kann wie man diesen Fehler loszuwerden.
Danke.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Hey, ich Verstand Ihr problem, Sie sind bereits mit einer Reihe von Bilder, warum verwenden imageNamed: Konstruktor wieder.
Direkt rausnehmen Bild aus array und weisen wie diese:
UIImage * ist wahrscheinlich nicht nötig.
self.selectedImages[row]
sollte einNSString
. Es scheint, wie es ist einUIImage
statt einerNSString
. Seine versuche zu nennen length-Methode auf dieUIImage
Instanz.