博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios ---常用的图片处理技术
阅读量:6324 次
发布时间:2019-06-22

本文共 4115 字,大约阅读时间需要 13 分钟。

本文转载至 http://blog.sina.com.cn/s/blog_8988732e0100xcx1.html
 
 
 
========== (one) UIImage 图像 等比例缩放 ==================================
PicAfterZoomWidth:缩放后图片宽  PicAfterZoomHeight:缩放后图片高 (预定义)

+ (UIImage *)getPicZoomImage:(UIImage *)image {

 

    UIImage *img = image;

    

    int h = img.size.height;

    int w = img.size.width;

    if(h <= PicAfterZoomWidth && w <= PicAfterZoomHeight)

    {

        image = img;

    }

    else 

    {

        float b = (float)PicAfterZoomWidth/w < (float)PicAfterZoomHeight/h ? (float)PicAfterZoomWidth/w : (float)PicAfterZoomHeight/h;

        CGSize itemSize = CGSizeMake(b*w, b*h);

        UIGraphicsBeginImageContext(itemSize);

        CGRect imageRect = CGRectMake(0, 0, b*w, b*h);

        [img drawInRect:imageRect];

        img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    }

    return img;

}

 

 =============== ( two )把图片 圆角 化==================================

 

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,

                                 float ovalHeight)

{

    float fw, fh;

    if (ovalWidth == 0 || ovalHeight == 0) {

        CGContextAddRect(context, rect);

        return;

    }

    CGContextSaveGState(context);

    CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));

    CGContextScaleCTM(context, ovalWidth, ovalHeight);

    fw = CGRectGetWidth(rect) / ovalWidth;

    fh = CGRectGetHeight(rect) / ovalHeight;

    CGContextMoveToPoint(context, fw, fh/2);  // Start at lower right corner

    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);  // Top right corner

    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner

    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner

    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right

    CGContextClosePath(context);

    CGContextRestoreGState(context);

}

+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size

{

    // the size of CGContextRef

    int w = size.width;

    int h = size.height;

    

    UIImage *img = image;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGRect rect = CGRectMake(0, 0, w, h);

    CGContextBeginPath(context);

    addRoundedRectToPath(context, rect, 10, 10);

    CGContextClosePath(context);

    CGContextClip(context);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

}

=============== (Three)给图片 添加阴影==================================

請先添加库 import QuartzCore.framework

然后要导入头文件 #import <QuartzCore/QuartzCore.h>

[[myView layer] setShadowOffset:CGSizeMake(5, 5)]; //设置阴影起点位置

[[myView layer] setShadowRadius:6];                       //设置阴影扩散程度

[[myView layer] setShadowOpacity:1];                      //设置阴影透明度

[[myView layer] setShadowColor:[UIColor blueColor].CGColor]; //设置阴影颜色

 ========== (Four) UIImage 图像 旋转==================================

- (UIImage *)imageRotatedByRadians:(CGFloat)radians

{

    return [self imageRotatedByDegrees:radians * 180/M_PI];

}

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees 

{   

    // calculate the size of the rotated view's containing box for our drawing space

    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];

    CGAffineTransform t = CGAffineTransformMakeRotation(degrees * M_PI / 180);

    rotatedViewBox.transform = t;

    CGSize rotatedSize = rotatedViewBox.frame.size;

    [rotatedViewBox release];

    // Create the bitmap context

    UIGraphicsBeginImageContext(rotatedSize);

    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image so we will rotate and scale around the center.

    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    

    //   // Rotate the image context

    CGContextRotateCTM(bitmap, degrees * M_PI / 180);

    // Now, draw the rotated/scaled image into the context

    CGContextScaleCTM(bitmap, 1.0, -1.0);

    CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

图像旋转更多请看:

你可能感兴趣的文章
上拉电阻的作用与选择
查看>>
(转)C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue 泛型
查看>>
ovs之组网实验
查看>>
union的使用(转)
查看>>
vue的表单的简单介绍 input radio checkbox等表单特性
查看>>
vue中自定义指令vue.direvtive,自定义过滤器vue.filter(),vue过渡transition
查看>>
django具体操作(七)
查看>>
俄总统参观卡巴斯基实验室 强调本土产品策略
查看>>
在ASP.NET MVC3中使用“.NET研究”EFCodeFirst 1.0
查看>>
互联网进病毒高发期 黑客瞄准网络春晚
查看>>
VMware workstation 下Hadoop伪分布式模式安装
查看>>
关于Android activity生命周期
查看>>
OpenModelica仿真
查看>>
robots.txt
查看>>
Redis客户端之Spring整合Jedis
查看>>
node服务搭建流程
查看>>
ASP.NET C# 访问Access、Xml等数据库
查看>>
python-logging模块详解
查看>>
Git -- 新增分支添加新功能
查看>>
SharePoint 2013 页面访问,Url中间多一段"_layouts/15/start.aspx#"
查看>>