[英]UIImage, check if contain image
如果我像這樣實例化 UIImage :
UIImage *image = [[UIImage alloc] init];
對象已創建,但不包含任何圖像。
如何檢查我的對象是否包含圖像?
您可以檢查它是否有任何圖像數據。
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")
}
在 Swift 3 中檢查cgImage
或ciImage
public extension UIImage {
public var hasContent: Bool {
return cgImage != nil || ciImage != nil
}
}
CGImage:如果 UIImage 對象是使用 CIImage 對象初始化的,則該屬性的值為 NULL。
CIImage:如果 UIImage 對象是使用 CGImageRef 初始化的,則該屬性的值為 nil。
檢查 CGImageRef 本身是否包含空值意味着 UIImage 對象不包含圖像......
這是@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;
}
UIImage 只能基於 CGImageRef 或 CIImage。 如果兩者都是 nil 表示沒有圖像。 給予方法的使用示例:
if (![self containsImage:imageview.image]) {
[self setImageWithYourMethod];
}
希望它會幫助某人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.