zoukankan      html  css  js  c++  java
  • 图片的裁剪

    1.将方形图片裁剪成圆形

    -(void)testImageClip

    {

        //加载图片

        UIImage *img = [UIImage imageNamed:@"123"];

        

        //开启上下文,跟图片尺寸一样大

        UIGraphicsBeginImageContextWithOptions(img.size, NO, 0);

        

        //设置裁剪区域

        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, img.size.width, img.size.height)];

        [path addClip];

        

        //绘制图片

        [img drawAtPoint:CGPointZero];

        

        //从上下文获取图片

        UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();

        

        //关闭上下文

        UIGraphicsEndImageContext();

        

        _imageV.image = clipImage;

        

    }

    2.给圆形再添加个边框

    @implementation UIImage (Clip)

    +(UIImage*)imageWithClipImage:(UIImage *)image borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth

    {

        CGFloat imageWH = image.size.height;

        

        CGFloat border = borderWidth;

        

        CGFloat ovaWH = imageWH ;

        

        UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovaWH, ovaWH), NO, 0  );

        

        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovaWH, ovaWH)];

        [color set];

        [path fill];

        //设置裁剪区域

        UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH - 2*border, imageWH - 2*border)];

        [clipPath addClip];

        

        //绘制图片

        [image drawAtPoint:CGPointZero];

        

        //从上下文获取图片

        UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();

        

        //关闭上下文

        UIGraphicsEndImageContext();

        

        return clipImage;

        

    }

    @end

     将图片按照原来的宽高比例压缩到与窗口合适的大小,然后在设置了_imageView.contentMode = UIViewContentModeCenter;  这个属性的UIImageView中展示压缩后的图片。

    //压缩图片  

    - (UIImage *)image:(UIImage*)image scaledToSize:(CGSize)newSize  

    {  

        // Create a graphics image context  

        UIGraphicsBeginImageContext(newSize);  

        // Tell the old image to draw in this new context, with the desired  

        // new size  

        [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  

        // Get the new image from the context  

        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();  

        // End the context  

        UIGraphicsEndImageContext();  

        // Return the new image.  

        return newImage;  

    }  

    按照窗口宽高比例,将原图横向或者纵向裁剪掉多余的部分,然后不设置UIImageView的contentMode属性,将裁剪后的图片送进去,使其自动适应窗口。

    //裁剪图片  

    - (UIImage *)cutImage:(UIImage*)image  

    {  

    //压缩图片  

        CGSize newSize;  

        CGImageRef imageRef = nil;  

    if ((image.size.width / image.size.height) < (bgImgView.size.width / bgImgView.size.height)) {  

            newSize.width = image.size.width;  

            newSize.height = image.size.width *bgImgView.size.height /bgImgView.size.width;  

            imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height));  

        } else {  

            newSize.height = image.size.height;  

            newSize.width = image.size.height *bgImgView.size.width / bgImgView.size.height;  

            imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));  

        }  

    return [UIImage imageWithCGImage:imageRef];  

    }  

     

    利用层裁剪

    @property (weak, nonatomic) IBOutlet UIImageView *imageV;

       _imageV.image = img;

       

    _imageV.layer.masksToBounds = YES;

        _imageV.layer.cornerRadius = img.size.width*0.5;

        _imageV.layer.borderWidth = 5;

        _imageV.layer.borderColor = [[UIColor redColor] CGColor];

  • 相关阅读:
    python里面的xlrd模块详解以及样例
    关于DOM的事件操作
    python正则表达式去除文本中间的换行符
    文本分类问题汇总
    pip安装问题
    3NF的无损连接和保持函数依赖的分解、BCNF的无损连接的分解
    Pyhton基本图形绘制
    软件过程模型
    常见算法的时间与空间复杂度
    随笔
  • 原文地址:https://www.cnblogs.com/PJXWang/p/5623616.html
Copyright © 2011-2022 走看看