UIImage, check if contain image

后端 未结 4 924
别那么骄傲
别那么骄傲 2020-12-24 01:51

If I instantiate a UIImage like this :

UIImage *image = [[UIImage alloc] init];

The object is created but it doesn\'t contain any image.

4条回答
  •  太阳男子
    2020-12-24 02:26

    This is updated version of @Kreiri, I put in a method and fixed logic error:

    - (BOOL)containsImage:(UIImage*)image {
        BOOL result = NO;
    
        CGImageRef cgref = [image CGImage];
        CIImage *cim = [image CIImage];
    
        if (cim != nil || cgref != NULL) { // contains image
            result = YES;
        }
    
        return result;
    }
    

    An UIImage could only based on CGImageRef or CIImage. If both is nil means there is no image. Example use of giving method:

    if (![self containsImage:imageview.image]) {
        [self setImageWithYourMethod];
    }
    

    hope it will help someone.

提交回复
热议问题