【问题标题】:UITableViewCell.ImageView.image Compare UIImageUITableViewCell.ImageView.image 比较 UIImage
【发布时间】:2014-10-18 11:27:33
【问题描述】:

我将 cell.imageView.image 与 UIImage 进行比较,如下代码。

请参考以下代码

    UIImage *imgOne = [UIImage imageNamed:@"One"];

    if([customCell.imageView.image isEqual: imgOne]){
          NSLog(@"Right Image");
    }

在调试时,我写为po [customCell.imageView.image isEqual imgOne],但它总是返回为'nil'。

谁能告诉我如何比较这个或正确的比较方法?

【问题讨论】:

  • :Tane prob che la? su thyu kahe 解决 kari apu。
  • 您使用的方法应该有效。这是比较图像的正确方法。您的调试结果应该返回 true 或 false,而不是 nil,因此肯定有其他问题。尝试记录 imgOne 和 customCell.imageView.image 以查看它们提供的内容(将该日志放在 if 语句的行上方,无论它在哪里)。
  • @Manthan :- Ha bhai .. 问题 6... 检查 j nath thatu。每次 e 条件 nil 返回 kare 6..
  • @rdelmar :- 谢谢你的建议,我试过了,但没用。
  • @ShreyanshShah:还是有问题??? Evu hoy 到聊天室可以解决 kari apu...

标签: ios objective-c uitableview uiimage


【解决方案1】:

一般UIImageView不能存储图片的文件名,但它只存储UIImage所以,不可能得到你在UIImageView中添加的图片的文件名。

这样,如果你只想比较图片文件名,那么就无法比较了。

但是如果你想比较两个UIImage 对象那么

UIImage *secondImage = [UIImage imageNamed:@"image.png"];

NSData *imgData1 = UIImagePNGRepresentation(self.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);

BOOL isCompare =  [imgData1 isEqualToData:imgData2];
if(isCompare)
{
  NSLog(@"Image View contains image.png");
}
else
{
  NSLog(@"Image View doesn't contains image.png");
}

【讨论】:

  • 我会尽快通知你的。
  • :- 感谢您宝贵的时间。这适用于演示项目,但不适用于我的原始应用程序..
【解决方案2】:

如果您将“图像一”保持为成员 var.(1)
并将单元格的图像设置为这个值。(2)
然后您可以使用这两个指针进行比较。(3)
请参阅此代码:

//1
self.imgOne  = [UIImage imageNamed:@"One"];  
//2  
customCell.imageView.image=self.imgOne   
//3  
if(customCell.imageView.image == self.imgOne)
   {}

【讨论】:

  • 如此简单快捷。我认为转换为 nsdata 是矫枉过正
【解决方案3】:

我尝试使用 Button

而不是 Imageview

比我检查如下

    if([cell.button.currentImage isEqual: [UIImage imagenamed:"Your Imagenem"]]){
NSLog(@"Right Image");

}

效果很好..

谢谢。

【讨论】:

    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多