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

2023-12-27

我有一个故事板应用程序,其中有一个UIViewController and a UICollectionViewController。在视图控制器中,用户从iPhone的照片库中选择多张照片(由于iOS中没有用于多选的API,所以我使用ELC图像选择器控制器 https://github.com/elc/ELCImagePickerController为了达成这个)。它继续到集合视图控制器,其中所选照片应显示在小图像视图中。

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

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

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

为了进一步演示这个问题,我制作了一个演示项目,您可以从以下位置下载here http://www20.zippyshare.com/v/63804660/file.html.

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

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

谢谢。


嘿,我理解你的问题,你已经有了一个图像数组,为什么再次使用 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 * 可能不需要。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

-[UIImage length]:无法识别的选择器发送到带有图像的 NSMutableArray 实例错误 的相关文章

  • CoreAnimation 性能分析 - CAReplicatorLayer 与 CAShapeLayer

    我正在制作一个依赖 CoreAnimation 的应用程序 它有一个 CAReplicatorLayer 和一个 CAShapeLayer 作为子层 当进行 12 次复制 然后对路径进行动画处理 在 touchMoved 上更改它 时 一旦
  • CNContact 添加新的联系人问题

    我在通过以下方式添加联系人时遇到问题联系框架 我使用的是装有 iOS 12 1 2 的 iPhone 5s 设备 我添加联系人的代码如下 let saveRequest CNSaveRequest saveRequest add self
  • 如何更改 UISwitch 关闭状态的默认颜色?

    我想更改 UISwitch 中 onTintColor 的颜色以表示关闭状态 切换位于表格视图中 并且以编程方式进行切换 settingsSwitch setBackgroundColor UIColor whiteColor settin
  • Xcode 不会在故事板中显示我的文本字段占位符文本

    当我在属性检查器中分配文本字段的占位符值时 它不会显示在故事板中 但是 当我运行应用程序的模拟器时 它就在那里 我缺少什么设置吗 我只想能够在编辑器中看到占位符文本 下面是 xcode 和模拟器之一的屏幕截图 我遇到了同样的问题 幸运的是我
  • 自动布局:Y 位置为两个值中的最大值

    我有一个按钮 play Button 和两个 UIView myView 1 和 myView 2 它们的位置在执行过程中可能会发生变化 我希望 playButton 的顶部比 UIView 1 的底部或 UIView 2 的底部低 10
  • Swift 3 '[UIApplicationLaunchOptionsKey:任意]?'无法转换为 '[String : NSString]'

    我有一个 TVOS 应用程序已从 Swift 2 转换为 Swift 3 但出现以下错误 我不确定如何让它安静下来 UIApplicationLaunchOptionsKey 任意 无法转换为 String NSString 它出现在这段代
  • ios 8 核心数据崩溃

    保存时 CoreData 发生崩溃 2014 09 16 09 51 58 273 My app 2678 105246 Terminating app due to uncaught exception NSInvalidArgument
  • 从 Plist 中存储和检索 [重复]

    这个问题在这里已经有答案了 可能的重复 iOS 在 plist 文件中存储两个 NSMutableArray https stackoverflow com questions 6070568 ios store two nsmutable
  • iOS Swift 检测键盘事件

    我能以某种方式检测来自 iOS 键盘的事件吗 我想检测此类事件UIViewController哪个没有UITextField或任何此类物体 我只有四个圆圈UIView我想在按下键盘上的按钮时将它们涂成不同的颜色 您没有任何对象可以从键盘获取
  • 在界面生成器/故事板中设置 UIButton 图像

    我有一个视图控制器 我在故事板中添加了一个圆形矩形按钮 该应用程序运行良好 我还使用故事板将按钮连接到 segue 我正在尝试为此按钮设置一个自定义图像以用于其开和关状态 我如何访问此按钮并设置其属性 在本例中为开和关图像 这是一个屏幕截图
  • 如何等待 webViewDidFinishLoad 完成

    我有一个初始化 webView 的布尔条件 并在 webViewDidFinishLoad 中加载另一个委托 以便在完成完成后触发 但是 由于布尔值在条件 webViewDidFinishLoad 之前返回 因此页面永远不会完全加载 如何确
  • 使用 Push Transition 效果更改 RootViewcontroller

    在我的iOS应用程序中 我需要更改应用程序之间窗口的rootviewController 因此 当我动态更改我的rootviewcontroller时 它会在更改之前轻拂视图 但我想要的是在更改rootviewcontroller时提供平滑
  • iOS 4.2.1 丢失文件?

    这是我第一次使用最新的 xcode 3 2 5 和新的 iOS 4 2 1 当我在设备上运行应用程序时 我收到以下运行时错误 无法读取 Developer Platforms iPhoneOS platform DeviceSupport
  • 使用 UIActionSheet 更改视图时工具栏项目消失

    当从 a 启动视图时UIActionSheet按钮 通过导航栏后退按钮返回视图后 工具栏虽然仍然可见 但上面没有任何以前的按钮 自从更新到 iOS 6 以来 这个错误就出现了 并且是在模拟器和仅运行 iOS 6 的设备上测试时发生的 如果我
  • iOS 外部附件框架:如何获取特定 MFI 设备的协议字符串

    我正在编写一个 iOS 应用程序 用于与 mini mPlay Drumi MP18B 小型蓝牙扬声器 进行通信 据我所知 showBluetoothAccessoryPickerWithNameFilter仅显示协议字符串添加到 Info
  • 在 iOS 中录制音频并永久保存

    我制作了 2 个 iPhone 应用程序 可以录制音频并将其保存到文件中并再次播放 其中之一使用 AVAudiorecorder 和 AVAudioplayer 第二个是苹果的在这里说话 http developer apple com l
  • iOS 中的等宽字体是什么?

    我想要在我的 iOS 应用程序中为 UILabel 使用等宽字体 不幸的是 我找不到一个 甚至 美国打字机 实际上也不是等宽的 XCode 中可用的等宽字体是什么 iOS 等宽字体 Courier Courier Bold Courier
  • iPad 3 中配备 Xcode 4.2 和 Retina 的 iOS 5.1

    我有一台装有 Mac OS X Snow Leopard 的 Mac 我可以添加 iOS 5 1 吗 使用 iPad 3 的新分辨率 我们将如何处理图像 因为如果该应用程序将在 iPhone 3GS 4 和 iPad 3 中运行 我认为我们
  • iOS 发送 iMessage 尽可能简单

    我希望能够以编程方式发送 iMessage 除了调用一个将文本发送到带有消息的号码的函数之外 无需执行任何其他操作 这两个消息都是文本框 我真的很感激一些示例代码 因为我在网上搜索过 但我发现没有任何帮助 这不适用于商业应用程序 仅适用于我
  • 如何使用 afnetworking 在后台上传任务

    我正在尝试使用 AFNetworking 上传大文件 并在应用程序处于后台时继续上传 我可以很好地上传文件 但是当我尝试使用后台配置时 应用程序崩溃并显示以下堆栈跟踪 异常 EXC BAD ACCESS 代码 1 地址 0x8000001f

随机推荐