【问题标题】:'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x1d53b6b0''NSInvalidArgumentException',原因:'-[UIImage 长度]:无法识别的选择器发送到实例 0x1d53b6b0'
【发布时间】:2013-04-02 10:27:13
【问题描述】:

我在相机叠加层上有一系列图像。当我单击按钮时,我需要使用相机视图拍摄一张叠加图像的快照。但是当我在设备中启动时出现以下错误。我搜索了一些无法得到答案的现有代码。

错误:

[UIImage length]: unrecognized selector sent to instance 0x1d53b6b0 2013-04-02 11:27:18.748 
    ARimage[1166:907] *** Terminating app due to uncaught exception 
    'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x1d53b6b0'

数组叠加图代码:

    NSArray *arrayOfImageFiles=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"img1.png"],
[UIImage imageNamed:@"img2.png"],
[UIImage imageNamed:@"img3.png"],
[UIImage imageNamed:@"img4.png"],
[UIImage imageNamed:@"img5.png"], nil];//array of images.

     for(NSString * imageFileName in arrayOfImageFiles)//here you are getting string format but imageFileName is an image object
          {
    UIImage * overlay = [UIImage imageNamed: imageFileName];//here imageFileName is image simply give here  UIImage * overlay=imageFileName;
    if(overlay)
        {

       CGSize overlaySize = [overlay size];                                                                    

       [overlay drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor, overlaySize.width * xScaleFactor, overlaySize.height * yScaleFactor)];                                                                  
        } else {                                                                                                                                      
         NSLog( @"could not find an image named %@", imageFileName);   

           }
          }                                                             
        UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();                          
         [self setStillImage:combinedImage];                                                           
           UIGraphicsEndImageContext();                                                     
             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];                                    

       }];                                                 
        }                          

为按钮点击捕获覆盖图像:

 - (void)ButtonPressed {

    [self captureStillImageWithOverlay:[NSArray arrayWithObjects:[UIImage imageNamed:@"img1.png"],
[UIImage imageNamed:@"img2.png"],
[UIImage imageNamed:@"img3.png"],
[UIImage imageNamed:@"img4.png"],
[UIImage imageNamed:@"img5.png"], nil]];                                                          

         }   

图像显示在相机叠加视图中。当我单击按钮捕获叠加图像时,出现错误。

【问题讨论】:

  • 一次检查这一张 UIImage * overlay = [UIImage imageNamed: imageFileName];在这个 imageFileName 从数组中获取但在数组中你已经将对象添加为图像。所以这里两次分配
  • @Sunny:我没听懂你。解释清楚
  • 我已经编辑了你的代码,一旦检查过。
  • @Sunny: 添加 UIImage * overlay=imageFileName;而是 UIImage * overlay = [UIImage imageNamed: imageFileName];我说的对吗?
  • 是的,这是正确的。

标签: ios objective-c arrays nsarray overlay


【解决方案1】:

试试这样,

 NSArray *arrayOfImageFiles=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"img1.png"],
[UIImage imageNamed:@"img2.png"],
[UIImage imageNamed:@"img3.png"],
[UIImage imageNamed:@"img4.png"],
[UIImage imageNamed:@"img5.png"], nil];//array of images.

     for(NSString * imageFileName in arrayOfImageFiles)//here you are getting string format but imageFileName is an image object
          {
      UIImage * overlay=(UIImage *)imageFileName;
    if(overlay)
        {

       CGSize overlaySize = [overlay size];                                                                    

       [overlay drawInRect:CGRectMake(30 * xScaleFactor, 100 * yScaleFactor, overlaySize.width * xScaleFactor, overlaySize.height * yScaleFactor)];                                                                  
        } else {                                                                                                                                      
         NSLog( @"could not find an image named %@", imageFileName);   

           }
          }                                                             
        UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();                          
         [self setStillImage:combinedImage];                                                           
           UIGraphicsEndImageContext();                                                     
             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];                                    

       }];                                                 
        }

【讨论】:

  • 再次得到同样的错误。我需要用叠加图像拍照。我使用了 AVFoundation 框架。检查我的按钮按下代码。我还设置了 [self captureStillImageWithOverlay:[NSArray arrayWithObjects:[UIImage imageNamed:@"img1.png"], [UIImage imageNamed:@"img2.png"], [UIImage imageNamed:@"img3.png"], [UIImage imageNamed:@"img4.png"], [UIImage imageNamed:@"img5.png"], nil]];对吗?
【解决方案2】:

for 声明:

for(NSString * imageFileName in arrayOfImageFiles)

没有意义。 arrayOfImageFiles 中的对象是 UIImage 的实例,而不是 NSString。在这一行

   UIImage * overlay = [UIImage imageNamed: imageFileName];

您将 UIImage 作为字符串参数传递,但它永远不会起作用。

我认为您需要将代码更改为这样(使用新的数组初始化器语法):

NSArray *arrayOfImageFileNames = @[@"img1.png", @"img2.png", @"img3.png", @"img4.png", @"img5.png"];

for (NSString* imageFileName in arrayOfImageFileNames)
{  
    // The rest as per your code
}

【讨论】:

  • 此代码对于获取叠加快照是正确还是错误 - (void)ButtonPressed { [self captureStillImageWithOverlay:[NSArray arrayWithObjects:[UIImage imageNamed:@"img1.png"], [UIImage imageNamed:@" img2.png"], [UIImage imageNamed:@"img3.png"], [UIImage imageNamed:@"img4.png"], [UIImage imageNamed:@"img5.png"], nil]]; }
  • 我不知道。这些方法是否需要一个字符串或图像数组?
猜你喜欢
  • 2023-03-11
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多