如何屏蔽UIImageView?

Mc.*_*ver 96 mask objective-c uiimage ios

我试图用这样的东西掩盖图像:

图像被掩盖

你能帮帮我吗?

我正在使用此代码:

- (void) viewDidLoad {
    UIImage *OrigImage = [UIImage imageNamed:@"dogs.png"];
    UIImage *mask = [UIImage imageNamed:@"mask.png"];
    UIImage *maskedImage = [self maskImage:OrigImage withMask:mask];
    myUIIMage.image = maskedImage;
}
Run Code Online (Sandbox Code Playgroud)

Bar*_*ski 160

有一种更简单的方法.

#import <QuartzCore/QuartzCore.h>
// remember to include Framework as well

CALayer *mask = [CALayer layer];
mask.contents = (id)[[UIImage imageNamed:@"mask.png"] CGImage];
mask.frame = CGRectMake(0, 0, <img_width>, <img_height>);
yourImageView.layer.mask = mask;
yourImageView.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

对于Swift 4和plus,请遵循以下代码

let mask = CALayer()
mask.contents =  [ UIImage(named: "right_challenge_bg")?.cgImage] as Any
mask.frame = CGRect(x: 0, y: 0, width: leftBGImage.frame.size.width, height: leftBGImage.frame.size.height)
leftBGImage.layer.mask = mask
leftBGImage.layer.masksToBounds = true
Run Code Online (Sandbox Code Playgroud)

  • 你应该将layer属性设置为使用掩码,即**[yourImageView.layer setMasksToBounds:YES];** (6认同)
  • 对于 Swift 代码,当放入 mask.contents 时,应该使用 CGImage。不是[CG图像]。如果使用括号[],则表示数组。 (4认同)
  • 请注意,这是此解决方案的UIImageView所必需的.另一种解决方案仅适用于UIImage. (2认同)

Nic*_*ver 59

本教程使用此方法有两个参数:image并且maskImage,您必须在调用方法时进行设置.示例调用可能看起来像这样,假设方法在同一个类中并且图片在您的包中:

注意 - 令人惊讶的是,图像甚至不必具有相同的大小.

...
UIImage *image = [UIImage imageNamed:@"dogs.png"];
UIImage *mask = [UIImage imageNamed:@"mask.png"];

// result of the masking method
UIImage *maskedImage = [self maskImage:image withMask:mask];

...

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

    CGImageRef maskRef = maskImage.CGImage; 

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

    CGImageRef maskedImageRef = CGImageCreateWithMask([image CGImage], mask);
    UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef];

    CGImageRelease(mask);
    CGImageRelease(maskedImageRef);

    // returns new image with mask applied
    return maskedImage;
}
Run Code Online (Sandbox Code Playgroud)

在您提供代码后,我添加了一些数字作为注释,以供参考.你还有两个选择.整个事情是一种方法,你在某处调用.您不需要在其中创建图像:这会将方法的可重用性降低到零.

让代码正常工作 将方法头(1.)更改为

- (UIImage *)maskImageMyImages {
Run Code Online (Sandbox Code Playgroud)

然后,在改变变量名2

UIImage *maskImage = [UIImage imageNamed:@"mask.png"];
Run Code Online (Sandbox Code Playgroud)

该方法将返回您的蒙版图像,因此您必须在某个地方调用此方法.你能告诉我们你调用方法的代码吗?


Mar*_*ens 13

Swift 3 - 我找到的最简单的解决方案

@IBDesignable为此创建了一个,所以你可以立即在故事板上看到效果.

import UIKit

@IBDesignable
class UIImageViewWithMask: UIImageView {
    var maskImageView = UIImageView()

    @IBInspectable
    var maskImage: UIImage? {
        didSet {
            maskImageView.image = maskImage
            updateView()
        }
    }

    // This updates mask size when changing device orientation (portrait/landscape)
    override func layoutSubviews() {
        super.layoutSubviews()
        updateView()
    }

    func updateView() {
        if maskImageView.image != nil {
            maskImageView.frame = bounds
            mask = maskImageView
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

如何使用

  1. 创建一个新文件并粘贴上面的代码.
  2. 将UIImageView添加到故事板(如果需要,可以指定图像).
  3. 在Identity Inspector上:将自定义类更改为"UIImageViewWithMask"(上面的自定义类名称).
  4. 在属性检查器上:选择要使用的蒙版图像.

掩盖故事板

笔记

  • 您的蒙版图像应具有非黑色部分的透明背景(PNG).


小智 12

我使用CALayer或CGImageCreateWithMask尝试了两个代码,但没有一个对我不起作用

但我发现问题是png文件格式,而不是代码!!
所以只是分享我的发现!

如果你想使用

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage
Run Code Online (Sandbox Code Playgroud)

你必须使用没有 alpha通道的24位 png

如果你想使用CALayer蒙版,你必须使用(24位或8位)png alpha通道,其中你的png的透明部分将掩盖图像(对于平滑的渐变alpha蒙版...使用24位png与alpha通道)


ZYi*_*iOS 10

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

        CGImageRef maskImageRef = [maskImage CGImage];

        // create a bitmap graphics context the size of the image
        CGContextRef mainViewContentContext = CGBitmapContextCreate (NULL, maskImage.size.width, maskImage.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
        CGColorSpaceRelease(colorSpace);

        if (mainViewContentContext==NULL)
            return NULL;

        CGFloat ratio = 0;

        ratio = maskImage.size.width/ image.size.width;

        if(ratio * image.size.height < maskImage.size.height) {
            ratio = maskImage.size.height/ image.size.height;
        }

        CGRect rect1  = {{0, 0}, {maskImage.size.width, maskImage.size.height}};
        CGRect rect2  = {{-((image.size.width*ratio)-maskImage.size.width)/2 , -((image.size.height*ratio)-maskImage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}};


        CGContextClipToMask(mainViewContentContext, rect1, maskImageRef);
        CGContextDrawImage(mainViewContentContext, rect2, image.CGImage);


        // Create CGImageRef of the main view bitmap content, and then
        // release that bitmap context
        CGImageRef newImage = CGBitmapContextCreateImage(mainViewContentContext);
        CGContextRelease(mainViewContentContext);

        UIImage *theImage = [UIImage imageWithCGImage:newImage];

        CGImageRelease(newImage);

        // return the image
        return theImage;
}
Run Code Online (Sandbox Code Playgroud)

这适合我.


Ahm*_*adi 8

SWIFT 3 XCODE 8.1

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

    let maskRef = maskImage.cgImage

    let mask = CGImage(
        maskWidth: maskRef!.width,
        height: maskRef!.height,
        bitsPerComponent: maskRef!.bitsPerComponent,
        bitsPerPixel: maskRef!.bitsPerPixel,
        bytesPerRow: maskRef!.bytesPerRow,
        provider: maskRef!.dataProvider!,
        decode: nil,
        shouldInterpolate: false)

    let masked = image.cgImage!.masking(mask!)
    let maskedImage = UIImage(cgImage: masked!)

    // No need to release. Core Foundation objects are automatically memory managed.

    return maskedImage

}
Run Code Online (Sandbox Code Playgroud)

// 用来

testImage.image = maskImage(image: UIImage(named: "OriginalImage")!, withMask: UIImage(named: "maskImage")!)
Run Code Online (Sandbox Code Playgroud)