iOS中的圖片遮罩處理

光影遮罩

這里我分類(lèi)深色與淺色遮罩,效果是什么樣呢,可以看下面的圖片,第一張是原圖,后面是遮罩圖。


[圖片上傳中...(mask.png-77d2d6-1533714162606-0)]
[圖片上傳中...(mask02.png-40df11-1533714186966-0)]
mask02.png

深色遮罩

Simulator Screen Shot - iPhone SE - 2018-08-08 at 15.46.08.png

淺色遮罩

Simulator Screen Shot - iPhone SE - 2018-08-08 at 15.54.18.png

關(guān)鍵代碼

+(UIImage *)creatImageWithMaskImage:(UIImage *)MaskImage andBackimage:(UIImage *)Backimage{
    
    CGRect rect;
    if (MaskImage.size.height>1000.0||MaskImage.size.width>1000.0)
    {
        rect = CGRectMake(0,
                          0,
                          MaskImage.size.width,
                          MaskImage.size.height);
    }else{
        rect = CGRectMake(0,
                          0,
                          MaskImage.size.width * 4.0,
                          MaskImage.size.height * 4.0);
    }
    CGImageRef imageRef = CGImageCreateWithImageInRect([Backimage CGImage], rect);
    UIImage *cutIMG = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    //遮罩圖
    CGImageRef maskImage = MaskImage.CGImage;
    //原圖
    CGImageRef originImage = cutIMG.CGImage;
    CGContextRef mainViewContentContext;
    CGColorSpaceRef colorSpace;
    colorSpace = CGColorSpaceCreateDeviceRGB();
    // create a bitmap graphics context the size of the image
    mainViewContentContext = CGBitmapContextCreate (NULL,
                                                    rect.size.width,
                                                    rect.size.height,
                                                    8,
                                                    0,
                                                    colorSpace,
                                                    kCGImageAlphaPremultipliedLast);
    // free the rgb colorspace
    CGColorSpaceRelease(colorSpace);
    if (mainViewContentContext==NULL)
    {
//        NSLog(@"error");
    }
    
    CGContextClipToMask(mainViewContentContext,
                        CGRectMake(0,
                                   0,
                                   rect.size.width,
                                   rect.size.height),
                        maskImage);
    
    CGContextDrawImage(mainViewContentContext,
                       CGRectMake(0,
                                  0,
                                  rect.size.width,
                                  rect.size.height),
                       originImage);
    
    
    CGContextSetAllowsAntialiasing(mainViewContentContext, true);//說(shuō)是抗鋸齒,但貌似沒(méi)用
    CGContextSetShouldAntialias(mainViewContentContext, true);
    // Create CGImageRef of the main view bitmap content, and then
    // release that bitmap context
    CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
    
    CGContextRelease(mainViewContentContext);
    // convert the finished resized image to a UIImage
    UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
    // image is retained by the property setting above, so we can
    // release the original
    
    CGImageRelease(mainViewContentBitmapContext);
    return theImage;
    
}

首先處理新生的畫(huà)圖范圍

CGRect rect;
    if (MaskImage.size.height>1000.0||MaskImage.size.width>1000.0)
    {
        rect = CGRectMake(0,
                          0,
                          MaskImage.size.width,
                          MaskImage.size.height);
    }else{
        rect = CGRectMake(0,
                          0,
                          MaskImage.size.width * 4.0,
                          MaskImage.size.height * 4.0);
    }

然后我們需要把原圖和遮罩圖轉(zhuǎn)換為像素位圖

CGImageRef imageRef = CGImageCreateWithImageInRect([Backimage CGImage], rect);
    UIImage *cutIMG = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    //遮罩圖
    CGImageRef maskImage = MaskImage.CGImage;
    //原圖
    CGImageRef originImage = cutIMG.CGImage;

然后需要定義一個(gè)圖形上下文與色彩空間

CGImageRef originImage = cutIMG.CGImage;
    CGContextRef mainViewContentContext;
    CGColorSpaceRef colorSpace;
    colorSpace = CGColorSpaceCreateDeviceRGB();
    // create a bitmap graphics context the size of the image
    mainViewContentContext = CGBitmapContextCreate (NULL,
                                                    rect.size.width,
                                                    rect.size.height,
                                                    8,
                                                    0,
                                                    colorSpace,
                                                    kCGImageAlphaPremultipliedLast);
    // free the rgb colorspace
    CGColorSpaceRelease(colorSpace);

接下來(lái)是把原圖和遮罩圖的像素位圖畫(huà)到畫(huà)布上

CGContextClipToMask(mainViewContentContext,
                        CGRectMake(0,
                                   0,
                                   rect.size.width,
                                   rect.size.height),
                        maskImage);
    
    CGContextDrawImage(mainViewContentContext,
                       CGRectMake(0,
                                  0,
                                  rect.size.width,
                                  rect.size.height),
                       originImage);
    
    
    CGContextSetAllowsAntialiasing(mainViewContentContext, true);//說(shuō)是抗鋸齒,但貌似沒(méi)用
    CGContextSetShouldAntialias(mainViewContentContext, true);
    // Create CGImageRef of the main view bitmap content, and then
    // release that bitmap context
    CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
    
    CGContextRelease(mainViewContentContext);

注意要手動(dòng)去釋放已創(chuàng)建的指針。
最后是把合成后的像素位圖轉(zhuǎn)換為一般的圖片

    UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
    CGImageRelease(mainViewContentBitmapContext);

除此之外還有一種方式,也是官方文檔提到的

遮罩方法二

關(guān)鍵代碼

+(UIImage *)creatImageWithBackimage:(UIImage *)Backimage andMaskImage:(UIImage *)MaskImage
{
    CGImageRef maskImage = MaskImage.CGImage;
    //    CGImageRef bgimage  = BgImage.CGImage;
    CGImageRef  mask = CGImageMaskCreate(CGImageGetWidth(maskImage), CGImageGetHeight(maskImage), CGImageGetBitsPerComponent(maskImage),  CGImageGetBitsPerPixel(maskImage), CGImageGetBytesPerRow(maskImage), CGImageGetDataProvider(maskImage),NULL, false);
    
    CGImageRef imageWithAlpha = Backimage.CGImage;
    CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
    UIImage * newimage = [UIImage imageWithCGImage:masked];
    //    CGImageRelease(maskImage);
    //    CGImageRelease(imageWithAlpha);
    CGImageRelease(masked);
    return newimage;
}

從這里看的出來(lái),相對(duì)而言簡(jiǎn)短很多。
這里使用到的關(guān)鍵是

CGImageMaskCreate(size_t width, size_t height,size_t bitsPerComponent, size_t bitsPerPixel, size_t bytesPerRow, CGDataProviderRef provider, const CGFloat decode[], bool shouldInterpolate)
sizt_t:是定義的一個(gè)可移植性的單位,在64位機(jī)器中為8字節(jié),32位位4字節(jié)。
width:圖片寬度像素
height:圖片高度像素
bitsPerComponent:每個(gè)顏色的比特?cái)?shù),例如在rgba-32模式下為8
bitsPerPixel:每個(gè)像素的總比特?cái)?shù)
bytesPerRow:每一行占用的字節(jié)數(shù),注意這里的單位是字節(jié)
space:顏色空間模式,例如const CFStringRef kCGColorSpaceGenericRGB 這個(gè)函數(shù)可以返回一個(gè)顏色空間對(duì)象。
bitmapInfo:位圖像素布局,這是個(gè)枚舉
provider:數(shù)據(jù)源提供者
decode[]:解碼渲染數(shù)組
shouldInterpolate:是否抗鋸齒
intent:圖片相關(guān)參數(shù)

后面設(shè)置遮罩圖的透明通道

 CGImageRef imageWithAlpha = Backimage.CGImage;
    CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
    UIImage * newimage = [UIImage imageWithCGImage:masked];
    //    CGImageRelease(maskImage);
    //    CGImageRelease(imageWithAlpha);
    CGImageRelease(masked);

PS:第二種方法和第一種的效果是相反的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。