美文网首页iOS-开发菜鸟->大神开发
iOS 任意图片切成原图且可以控制填充方式

iOS 任意图片切成原图且可以控制填充方式

作者: e5311f1a36e5 | 来源:发表于2017-04-20 18:37 被阅读9次

    代码示例

    + (UIImage *)getRoundImageWithOriginalImage:(UIImage *)originalImage contentMode:(UIViewContentMode)contentMode rect:(CGRect)rect{
    
    //先切成正方形的图
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];
    tempImageView.contentMode = contentMode;
    tempImageView.frame = rect;
    tempImageView.backgroundColor = [UIColor whiteColor];
    
    //切取imageview上的图
    UIGraphicsBeginImageContextWithOptions(tempImageView.bounds.size, NO, 0);
    [tempImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIGraphicsBeginImageContextWithOptions(tempImage.size, NO, 0);
    
    //裁切范围
    UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, tempImage.size.width, tempImage.size.height)];
    [path addClip];
    
    //绘制图片
    [tempImage drawAtPoint:CGPointZero];
    
    //从上下文中获得裁切好的图片
    UIImage *tempImage1 = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图片上下文
    UIGraphicsEndImageContext();
    return tempImage1;
    }

    相关文章

      网友评论

        本文标题:iOS 任意图片切成原图且可以控制填充方式

        本文链接:https://www.haomeiwen.com/subject/ttwlzttx.html