【问题标题】:Check if UIImage returned from NINetworkImageView is valid检查从 NINetworkImageView 返回的 UIImage 是否有效
【发布时间】:2011-11-30 18:17:37
【问题描述】:

所以我正在调用使用 Nimbus 的 NINetworkImageView 对象从照片服务器中提取图像。我需要检查返回的对象是否有效。如果无效,则方法

- (void) networkImageView:(NINetworkImageView *) imageView didLoadImage:(UIImage *) image {

返回一个透明的 UIImage 对象。我认为该方法返回给我一个带有清晰像素的 jpg 图像,这导致了我的问题。我尝试了以下检查以查看图像是否透明但没有运气(以下所有尝试都发生在上述方法的范围内):

尝试 1 失败:

UIImageView *tempView = [[UIImageView alloc] initWithImage:image]; 
if(tempView.alpha != 0.0f)
{   
    //the image is a valid (nonclear) image
}
else
{
     //the image is invalid (clear)
}

尝试 2 失败:

CGImageRef cgref = [[UIImage alloc] CGImage];
CIImage *cim = [[UIImage alloc] CIImage];

if(!([image CIImage] == cim && [image CGImage] == cgref))
{   
     //the image is a valid (nonclear) image
}
else
{
     //the image is invalid (clear)
}

尝试 3 失败:

if(image)
{   
     //the image is a valid (nonclear) image
}
else
{
     //the image is invalid (clear)
}

尝试 4 失败:

CGImageRef cgref = [image CGImage];
CIImage *cim = [image CIImage];

if(cim == nil && cgref == NULL)
{         
    //the image is invalid (clear)  
}
else
{
    //the image is a valid (nonclear) image
}

编辑

我能够从模拟器中提取“无效图像”。 networkimageview 方法返回一个完全白色的 jpg 图像。但是,在模拟器中它显示为清晰的图片,想法?

【问题讨论】:

  • 无效是什么意思? Nimbus 文档说还有委托方法networkImageViewDidFailLoad: - 你的情况是这样吗?或者你的意思是返回了数据,但是图片加载失败。在这种情况下,我希望imagenilimage.sizeCGSizeZero
  • 对不起,我的意思是如果图像是空白的,它是“无效的”。图像加载得很好,但图像很清晰。使用@beryllium 指令我能够检查下载的图像。这是一张没有 Alpha 通道的 JPG 图像,但由于某种原因,它显示为空白图像。

标签: iphone ios uiimage nimbus-ios


【解决方案1】:

我可以建议将此图像保存到磁盘并在 Photoshop/其他图形工具中查看它的 alpha 值。

UIImageWriteToSavedPhotosAlbum(image, nil, nil, NULL);

现在您可以在照片应用中找到这张图片。

以下函数将名为image.png的UIImage文件保存在用户Document文件夹中:

- (void)saveImage: (UIImage*)image
{
    if (image != nil)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                      NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                       [NSString stringWithString: @"test.png"] ];
        NSData* data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
    }
}

【讨论】:

  • 我去哪里获取照片?我在 ~/Library/Application Support/iPhone Simulator/media/ 但照片在 sql 文件中。
  • 嗯。我真的很困惑如何在模拟器中做到这一点。在设备上,我只是用 ImageCapture App 抓取它
  • @Endama,我已经更新了答案。此代码允许您在 Documents 目录中查找图像
  • 没关系我找到了,你需要去iPhoneSimulator文件夹中的DCIM文件夹。我检查了图像,它是jpg,它是白色的。但是,在应用程序中使用时,它显示为清晰。
  • 它没有 Alpha 通道。
【解决方案2】:

我找到了解决办法。使用 Beryllium 的建议,我下载了 jpg 图像,然后将其添加到 XCODE 项目中。我将其命名为 BlankImage.jpg 并添加了它。然后我做以下比较

- (void) networkImageView:(NINetworkImageView *) imageView didLoadImage:(UIImage *) image 
{

    if([ UIImageJPEGRepresentation([UIImage imageNamed:@"BlankImage.JPG"], 1.0f ) isEqualToData: UIImageJPEGRepresentation(image, 1.0f ) ])
    {
           //This is a blank image so handle that case
    }
    else
    {
           //This is a valid image
    }
}

另一种选择是为逐像素比较做疯狂的石英材料。这工作得很好。

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 2015-03-05
    • 2016-08-20
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2013-10-12
    相关资源
    最近更新 更多