Check if UIImage returned from NINetworkImageView is valid

730 views Asked by At

So I am making a call to pull an image from a photo server using Nimbus' NINetworkImageView object. I need to check if the object returned is valid. If it is not valid, the method

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

returns a transparent UIImage object. I think the method is returning me a jpg image with clear pixels which is causing me the issue. I have tried the following checks to see if the image is transparent with no luck (All of the attempts below happen in the scope of the method above):

Failed Attempt 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)
}

Failed Attempt 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)
}

Failed Attempt 3:

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

Failed Attempt 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
}

Edit

I was able to pull the "Invalid image" from the simulator. The networkimageview method is returning a jpg image that is completely white. However, in the simulator it shows up as a clear picture, thoughts?

2

There are 2 answers

6
beryllium On BEST ANSWER

I can advice to save this image to disk and see it's alpha value in Photoshop/another graphic tool.

UIImageWriteToSavedPhotosAlbum(image, nil, nil, NULL);

Now you can find this image in Photos app.

The following function saves UIImage with name image.png file in the user Document folder:

- (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];
    }
}
0
Endama On

I found the fix. Using Beryllium's suggestion I downloaded the jpg image and then added it to the XCODE project. I named it BlankImage.jpg and added it. I then do the following comparison

- (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
    }
}

The other option was to do crazy quartz stuff for pixel by pixel comparison. This worked just fine.