2011-11-18 88 views

回答

64

您可以檢查它是否有任何圖像數據。

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

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

if (cim == nil && cgref == NULL) 
{ 
    NSLog(@"no underlying data"); 
} 
[image release]; 
+0

非常感謝,這是完美的工作! – Jonathan

+0

但[圖像CIImage];僅適用於iOS 5.關於4.3的情況? –

+4

不工作作爲cim返回零 – hii

0

檢查與CGImageRef與自身wheather它包含空值意味着UIImage對象不包含圖像.......

8

檢查cgImageciImage,在夫特3

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.

CIImage: If the UIImage object was initialized using a CGImageRef, the value of the property is nil.

+0

可以請你給SWIFT更新3 –

+0

@AnkitKumarGupta完成 – onmyway133

0

這是@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。如果兩者都是零意味着沒有圖像。舉例使用賦予方法:

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

希望它能幫助別人。