-[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 实例错误 的相关文章

  • 是否可以使用 Firebase 安排推送通知? [复制]

    这个问题在这里已经有答案了 我已经阅读了我能找到的所有文档 但仍然不知道这是否可行 如果我是用户 我可以安排特定时间的推送通知吗 Example 1 我是用户并打开应用程序 2 我允许通知并转到 pickerView 或其他任何内容 并设置
  • 更改组织以使用 Xcode 9 在 iTunes Connect 上上传二进制文件

    我在 Xcode9 上配置了多个团队 当我尝试将二进制文件上传到 Xcode 9 上的 iTunes Connect 时 没有更改团队的选项 并且出现以下错误 ERROR ITMS 4088 来自苹果开发者论坛的解决方案 1 正常存档2 窗
  • 减少 CoreData 的调试输出?

    我正在开发一个使用 CoreData 的 iOS macOS 项目 它工作正常 但它会向控制台输出大量调试信息 这使得控制台无法使用 因为我的打印语句隐藏在所有与 CoreData 相关的内容中 我有一个非常简单的 CoreData 设置
  • 如何让按钮闪烁?

    我试图在扫描正确时将按钮的颜色 只是闪烁 闪烁 更改为绿色 在出现问题时将按钮的颜色更改为红色 我可以用这样的视图来做到这一点 func flashBG UIView animateWithDuration 0 7 animations s
  • iOS - UITableViewCell 使文本加粗

    我有一个字符串 NSString userInfo James Johnson james 我想做的就是大胆James Johnson并保留 james正常字体 所以我尝试过的是使用NSAttributedString但为了完成这个过程 我
  • 当 isUserInteractionEnabled false 时,SKSpriteNode 不会让触摸通过

    我正在尝试在 SpriteKit 中创建一个覆盖层 方法是使用SKSpriteNode 但是 我希望触摸穿过覆盖层 所以我设置isUserInteractionEnabled为假 然而 当我这样做时 SKSpriteNode似乎仍然吸收所有
  • 从未调用过交互式委托方法

    我想在 ViewController 1 和 NavigationViewController 2 之间进行交互式转换 NavigationController 通过按钮调用 因此呈现时没有交互转换 它可以通过按钮或 UIPanGestur
  • 如何为 iPhone 6+、6 和 5 指定不同尺寸?

    我想让 iPhone 6 6 和 5 上的视图看起来几乎相同 在附图中 我的意思是 例如 取消 按钮在 iPhone 5 中距离屏幕左边缘应为 30 像素 在 6 中为 35 像素 在 6 中为 45 像素 其他元素也类似 如何为每种类型设
  • 在 iOS 7 中 viewForHeaderInSection 部分是从 1 开始而不是从 0 开始

    我正在处理UITableView在我的项目中 这个项目是在 Xcode 4 5 中创建的 现在我正在使用 Xcode 5 所以我的问题是何时在 iOS 6 中运行我的项目 viewForHeaderInSection方法部分从 0 开始没问
  • 使用数组中的字符串淡入/淡出标签

    func setOverlayTitle self overlayLogo text Welcome var hello String Bon Jour GUTEN nMORGEN BONJOUR HOLA BUENOS D AS BUON
  • UIViewController 不旋转到横向

    在许多情况下需要旋转控制器但不起作用 现在我遇到了相反的问题 它正在旋转 我想禁用它 在那个 ViewController 中我有这个 BOOL shouldAutorotateToInterfaceOrientation UIInterf
  • iOS 7 tabBar 横线,如何去掉?

    Apple 在 iOS 7 中的 tabBar 上添加了一条细线 该线应该在 tabBar 和 UI 之间起到阴影或淡入淡出的作用 由于我使用的是定制的 tabBar 这条线非常令人恼火 你如何删除它 请告诉我这是可能的 否则我需要重新设计
  • CATextLayer 上 iOS 6 中不需要的垂直填充

    背景 我在 iOS 5 中开始了我的项目 并构建了一个带有图层的漂亮按钮 我在按钮上添加了一个 textLayer 并使用以下代码将其居中 float textLayerVerticlePadding self bounds size he
  • 如何使用 IOS 12 在 UITableViewCell 中正确添加 UICollectionView

    由于某些原因 在使用 Xcode 10 beta 时 我无法正确显示 tableview 单元格内集合中的某些项目 在过去的四天里我尝试了我所知道的一切 我做了一个小项目样本来看看我的问题是什么 如果有人想在本地运行完整代码 请参见此处 h
  • Xcode 异步单元测试在主线程上等待

    我正在尝试使用 Xcode 中的单元测试来测试一些异步代码 但主线程被阻塞 问题在于 某些正在测试的代码期望从 iOS 类 AVFoundation 接收回调 但是 AVFoundation 类似乎只会在主线程上回调 问题是 如果我正在进行
  • 从现有坐标地图套件中查找最近的位置

    我正在为拥有多家商店的客户开发 iPhone 应用程序 目标 C 我有数组中所有商店 20 的坐标 纬度 长 目前我正在考虑循环遍历商店坐标数组并获取从用户当前位置到商店位置的距离 然后将它们添加到数组中并按最小距离进行排序 这是正确的方法
  • iOS:Facebook 登录访问令牌错误:由于模拟器错误,回退到从 NSUserDefaults 加载访问令牌

    根据说明进行配置后 我不断收到此错误 并且无法在我的应用程序上成功使用 Facebook 登录 我在 XCode 8 1 上运行它并使用 iOS 10 1 模拟器 我按照 Facebook iOS SDK 指南中的步骤操作 并将 Faceb
  • 上传存档错误:“缺少 iOS 发行版签名身份......”

    我正在尝试使用 Xcode 将我的 iOS 应用程序存档上传到 iTunes Connect 但是当我单击 上传到 App Store 时 出现错误 Xcode 尝试查找或生成匹配的签名资产并 由于以下问题未能做到这一点 缺少 iOS 为
  • 如何在 UITableView 的 switch 语句中创建变量?

    我正在构建一个包含三个部分的 tableView 我已经完成了前两个工作 但最后一个有点阻力 我的问题似乎涉及尝试在 switch 语句中声明变量 实际上是嵌套的 switch 语句 据我所知 这不是一个好主意 但在这种情况下 这似乎是唯一
  • 节拍匹配算法

    我最近开始尝试创建一个移动应用程序 iOS Android 它将自动击败比赛 http en wikipedia org wiki Beatmatching http en wikipedia org wiki Beatmatching 两

随机推荐