2010-06-17 118 views
26

我正在一個應用程序,我通過代碼繪製UIView,並用UIColor填充它。接下來我想加載一個mask.png文件(不透明,只是黑白),並掩蓋UIView以改變其視覺外觀。如何掩蓋UIView

任何想法如何做到這一點?

回答

5

您可能能夠使用CGContextClipToMask:

-(void) drawRect:(CGRect)dirty { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 
    CGContextClipToMask(context , [self bounds] , [self maskImage]); 
    [super drawRect:dirty]; // or draw your color manually 
    CGContextRestoreGState(context); 
} 
3

感謝drawnonward,

結束了此功能:

- (UIImage*) maskImage:(UIView *)image withMask:(UIImage *)maskImage { 

    UIGraphicsBeginImageContext(image.bounds.size); 
    [image.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 


    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
             CGImageGetHeight(maskRef), 
             CGImageGetBitsPerComponent(maskRef), 
             CGImageGetBitsPerPixel(maskRef), 
             CGImageGetBytesPerRow(maskRef), 
             CGImageGetDataProvider(maskRef), NULL, false); 

    CGImageRef masked = CGImageCreateWithMask([viewImage CGImage], mask); 
    return [UIImage imageWithCGImage:masked]; 

} 
51
// Create your mask layer 
CALayer* maskLayer = [CALayer layer]; 
maskLayer.frame = CGRectMake(0,0,yourMaskWidth ,yourMaskHeight); 
maskLayer.contents = (__bridge id)[[UIImage imageNamed:@"yourMaskImage.png"] CGImage]; 

// Apply the mask to your uiview layer   
yourUIView.layer.mask = maskLayer; 

記得導入QuartzCore並添加框架

#import <QuartzCore/QuartzCore.h> 

這很容易,但我沒有在任何地方找到答案!

+1

應該首選任何使用Core Animation而不是Core Graphics的答案。 CA的API更易於理解和使用。 – zakdances 2013-04-24 05:15:37

4

我已創建與上述代碼的類別:

的UIView + ImageMask.h:

#import <UIKit/UIKit.h> 

@interface UIView (ImageMask) 

- (void) maskWithImage:(UIImage*) mask; 
- (void) maskWithImage:(UIImage*)mask size:(CGSize)maskSize; 
- (void) removeMask; 

@end 

的UIView + ImageMask.m:

#import <QuartzCore/QuartzCore.h> 

#import "UIView+ImageMask.h" 

@implementation UIView (ImageMask) 

- (void) maskWithImage:(UIImage*) mask { 
    [self maskWithImage:mask size:mask.size]; 
} 

- (void) maskWithImage:(UIImage*)mask size:(CGSize)maskSize { 
    CALayer* maskLayer = [CALayer layer]; 
    maskLayer.frame = CGRectMake(0, 0, maskSize.size.width, maskSize.size.height); 
    maskLayer.contents = (__bridge id)[mask CGImage]; 

    // Apply the mask to your uiview layer 
    self.layer.mask = maskLayer; 
} 

- (void) removeMask { 
    self.layer.mask = nil; 
} 

@end 
+0

你有什麼樣的圖像輸入?黑色或白色或半透明,白色或半透明和黑色? – Vassily 2014-02-20 16:53:40

+0

我用一個黑色的圖像與阿爾法。但我不認爲這會有所作爲。如果你嘗試它,讓我知道:)。 – mt81 2014-02-20 20:16:33

+0

我試過了。其實有區別。只有alpha分量纔會生效 – Arsynth 2014-02-27 15:09:57

11

iOS 8的介紹maskView屬性它允許您使用替代視圖來提供遮罩圖像。在Swift中:

if let maskImage = UIImage(named: "MaskImage") { 
    myView.maskView = UIImageView(image: maskImage) 
} 

請注意,您需要提供實際具有透明度的圖像;具有白色部分(用於不透明)和黑色部分(用於透明)的圖像將不起作用。

+0

任何想法爲什麼這可能會崩潰與EXC_BAD_ACCESS? ( - [UIImageView _maskView]:發送到釋放實例的消息) – Yaroslav 2016-05-07 18:17:38

0

雨燕2.0

傳遞一個UIImage和遮掩圖像這個功能,你會得到一個遮蓋的圖像作爲一個UIImage。

func maskImage(image:UIImage, mask:(UIImage))->UIImage{ 

    let imageReference = image.CGImage 
    let maskReference = mask.CGImage 

    let imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference), 
            CGImageGetHeight(maskReference), 
            CGImageGetBitsPerComponent(maskReference), 
            CGImageGetBitsPerPixel(maskReference), 
            CGImageGetBytesPerRow(maskReference), 
            CGImageGetDataProvider(maskReference), nil, true) 

    let maskedReference = CGImageCreateWithMask(imageReference, imageMask) 

    let maskedImage = UIImage(CGImage:maskedReference!) 

    return maskedImage 
}