CAShapeLayer 局部区域暗色处理 iOS图片裁剪处理

来源:互联网 发布:网络战 编辑:程序博客网 时间:2024/05/12 09:43

- (void)viewDidLoad

{

    [superviewDidLoad];


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

    [self.view.layersetContents:(id)image.CGImage];

    //先遮挡全部

    UIView *holeView = [[UIView alloc] initWithFrame:self.view.bounds];

    holeView.backgroundColor = [UIColorcolorWithWhite:0alpha:0.5];

    [self.viewaddSubview:holeView];

    

    CAShapeLayer *layer = [CAShapeLayerlayer];

    //高亮区域

    UIBezierPath *path = [UIBezierPathbezierPathWithRect:CGRectMake(0,100, 320, 300)];

    //后面的一个path 为背景path'

    [path appendPath:[UIBezierPathbezierPathWithRect:self.view.bounds]];

    layer.path = path.CGPath;

    //最主要的就是这个模式。fillRule有两种种模式

    layer.fillRule =kCAFillRuleEvenOdd;

    

    [holeView.layersetMask:layer];

}


//////////图片裁剪///////////

- (UIImage *)output

{

    // 获取到需要裁剪的frame

    CGRect rectclip = [self convertRect:imageViewCase.frametoView:imageViewBg];  

    //获取到cgimage裁剪到的图片  

    CGImageRef subImageRef = CGImageCreateWithImageInRect(_editImage.CGImage, rectclip);

    //创建一个context画板

    CGContextRef context =UIGraphicsGetCurrentContext();

   // 图片画入画板

    CGContextDrawImage(context, rectclip, subImageRef);

   //画板中获取到图片

    UIImage *viewImage = [UIImage imageWithCGImage:subImageRef]; 

    //关闭画板

    UIGraphicsEndImageContext();

    return viewImage;


}


///////图片裁剪,获取到原图比例出片/////


- (UIImage *)croppedImage:(UIImage *)image andbeiS:(int)beis

{

    if (image)

    {

        float minW = image.size.width/beis;

        float minH = image.size.height/beis;

        CGRect rectMAX = CGRectMake(0,0, minW, minH);

        UIGraphicsBeginImageContext(rectMAX.size);//创建出一个size的大小的画板

        [image drawInRect:rectMAX];//image写入size中

        UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return viewImage;

    }

    

    return nil;

    

}



0 0
原创粉丝点击