iOS图片处理相关

最近公司项目要做图片处理这里总结一下:

1.普通上下文

CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSaveGState(ref);
XXXXX
XXXXX
CGContextRestoreGState(ref);

2.遮罩处理

利用图层

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.fillMode = kCAFillRuleEvenOdd;
// UIBezierPath *maskLayerPath = [UIBezierPath bezierPath];
// [maskLayerPath appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, 100, 100)]];
// maskLayer.path = maskLayerPath.CGPath;
maskLayer.contents = (__bridge id _Nullable)(maskImage.CGImage);
[self.layer setMask:maskLayer];

3.剪切遮罩处理(针对图片)

1.CGContextClipToMask(ref, self.bounds, imgRef);

按图片剪裁context,alpha=0干掉,alpha=1留下;
参数 CGContextRef Rect CGImageRef

2.CGImageCreateWithMask([image CGImage], mask);

两张图片,mask处理,mask图片alpha=1,纯白表示透明, 纯黑表示要留下, 其他颜色,相应混合处理

CGImageCreateWithMaskingColors([image CGImage], CGFloat _UnNull maskColor);

一张图片,按colorspace的要求进行处理

- (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
    
    //    UIImage *blackImg = [self imageBlackToTransparent:maskImage];
    //    CGImageRef maskRef = blackImg.CGImage;
    
    CGImageRef maskRef = maskImage.CGImage;
    
    maskRef = [PuzzleView maskableImageRefConvertFrom:maskRef];
    
    //    const CGFloat maskcolor[6] = {0,0,0,0,0,0};
    //    CGImageRef maskRef = CGImageCreateWithMaskingColors(maskImage.CGImage, maskcolor);
    
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);
    
    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
    return [UIImage imageWithCGImage:masked];

    //    UIImage *blackImg = [self imageBlackToTransparent:maskImage];
    //    CGImageRef maskRef = blackImg.CGImage;
    //    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
    
//        CGImageGetHeight(maskRef),
//
//        CGImageGetBitsPerComponent(maskRef),
//
//        CGImageGetBitsPerPixel(maskRef),
//
//        CGImageGetBytesPerRow(maskRef),
//        CGImageGetDataProvider(maskRef), NULL, false);
    //    CGImageRef sourceImage = [image CGImage];
    //    CGImageRef imageWithAlpha = sourceImage;
    //    [self imageDump:maskImage];
    
    //    if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) {
    //        imageWithAlpha = [self CopyImageAndAddAlphaChannel:sourceImage];
    //    }
    //    CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
    //    CGImageRelease(mask);
    //    if (sourceImage != imageWithAlpha) {
    //        CGImageRelease(imageWithAlpha);
    //    }
    //    UIImage* retImage = [UIImage imageWithCGImage:masked];
    //    CGImageRelease(masked);
    //    return retImage;
}

4.关于截屏失真

- (IBAction)saveBtnTap:(UIButton *)sender {
// 这个方法将scale 变为屏幕 scale,默认1.0.  改为屏幕scale后变为屏幕分辨率
    UIGraphicsBeginImageContextWithOptions(self.mainView.bounds.size, NO, [UIScreen mainScreen].scale);
    [self.mainView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"保存成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alertView show];
}

你可能感兴趣的:(iOS图片处理相关)