[英]UIImage, check if contain image
If I instantiate a UIImage like this :如果我像这样实例化 UIImage :
UIImage *image = [[UIImage alloc] init];
The object is created but it doesn't contain any image.对象已创建,但不包含任何图像。
How I can check if my object contains an image or not?如何检查我的对象是否包含图像?
You can check if it has any image data.您可以检查它是否有任何图像数据。
UIImage* image = [[UIImage alloc] init];
CGImageRef cgref = [image CGImage];
CIImage *cim = [image CIImage];
if (cim == nil && cgref == NULL)
{
NSLog(@"no underlying data");
}
[image release];
let image = UIImage()
let cgref = image.cgImage
let cim = image.ciImage
if cim == nil && cgref == nil {
print("no underlying data")
}
Check for cgImage
or ciImage
, in Swift 3在 Swift 3 中检查
cgImage
或ciImage
public extension UIImage {
public var hasContent: Bool {
return cgImage != nil || ciImage != nil
}
}
CGImage: If the UIImage object was initialized using a CIImage object, the value of the property is NULL.
CGImage:如果 UIImage 对象是使用 CIImage 对象初始化的,则该属性的值为 NULL。
CIImage: If the UIImage object was initialized using a CGImageRef, the value of the property is nil.
CIImage:如果 UIImage 对象是使用 CGImageRef 初始化的,则该属性的值为 nil。
检查 CGImageRef 本身是否包含空值意味着 UIImage 对象不包含图像......
This is updated version of @Kreiri, I put in a method and fixed logic error:这是@Kreiri 的更新版本,我输入了一个方法并修复了逻辑错误:
- (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. UIImage 只能基于 CGImageRef 或 CIImage。 If both is nil means there is no image.
如果两者都是 nil 表示没有图像。 Example use of giving method:
给予方法的使用示例:
if (![self containsImage:imageview.image]) {
[self setImageWithYourMethod];
}
hope it will help someone.希望它会帮助某人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.