美文网首页
图片的简单处理

图片的简单处理

作者: oncezou | 来源:发表于2016-08-11 11:40 被阅读176次

需求:很多时候我们需要对拍照后的图片进行处理展示或者上传,但是原图的数据是很大,或者我们要展示的区域很小,这时我们就需要对图片进行简单的处理

做法:1.原图originalImage进行上传
2.缩略图thumImage进行展示

       //image是通过系统相机代理方法回调获取拍照的原图
        UIImage *originalImage = image;
        
        //计算比例和长宽
        CGFloat rate = 200.0/266.0;
        
        CGFloat cw = originalImage.size.width;
        
        CGFloat h = originalImage.size.height;
        
        CGFloat ch = originalImage.size.width*rate;

        //originalImagePath-原图的本地存放路径
        NSString *originalImagePath = photo.originalImagePath;

        //thumImagePath-缩略图的本地存放路径
        NSString *thumImagePath = photo.thumImagePath;
    
        //按比例截取中间部分
        UIImage *thumImage = [originalImage getSubImage:CGRectMake(0, (h-ch)/2.0, cw, ch)];
    
        //等比例压缩
        thumImage = [thumImage scaleToSize:CGSizeMake(266.0, 200.0)];
        
        /*
         * UIImageJPEGRepresentation与UIImagePNGRepresentation的区别:
         * 1.UIImageJPEGRepresentation处理的图片比UIImagePNGRepresentation小(这是很重要的,上传图片我们考虑的就是体积大小和图片质量,如果上传的图片过大肯定是不好的)
         * 2.UIImageJPEGRepresentation有一个缩放系数,可以将图片缩放到能接受的质量,一般在0.3-0.7之间,从而也减小了了图片的大小,而UIImagePNGRepresentation没有这个系数
         */
        //写入文件
        NSData *imagedata = UIImageJPEGRepresentation(originalImage,0.6);
        
        [imagedata writeToFile:originalImagePath atomically:YES];
        
        NSData *thumImagedata = UIImageJPEGRepresentation(thumImage,1);
        
        [thumImagedata writeToFile:thumImagePath atomically:YES];

上面两个是用于展示剪切后的缩略图的方法:
1.- (UIImage)getSubImage:(CGRect)rect //截取部分图像
2.- (UIImage
)scaleToSize:(CGSize)size //等比例缩放

将其抽成UIImage的分类,具体实现:

//处理图片方向(比如横拍,反拍等最后都转成正常方向展示)
- (UIImage *)fixOrientation {
    
    // No-op if the orientation is already correct
    if (self.imageOrientation == UIImageOrientationUp)
        return self;
    
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (self.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (self.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                             CGImageGetBitsPerComponent(self.CGImage), 0,
                                             CGImageGetColorSpace(self.CGImage),
                                             CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

//截取部分图像
- (UIImage*)getSubImage:(CGRect)rect
{
    UIImage *image = [self fixOrientation];
    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    CGImageRelease(subImageRef);
    
    return smallImage;
}

//等比例缩放
- (UIImage*)scaleToSize:(CGSize)size
{
    CGFloat width = CGImageGetWidth(self.CGImage);
    CGFloat height = CGImageGetHeight(self.CGImage);
    
    float verticalRadio = size.height*1.0/height;
    float horizontalRadio = size.width*1.0/width;
    
    float radio = 1;
    if(verticalRadio>1 && horizontalRadio>1)
    {
        radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;
    }
    else
    {
        radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
    }
    
    width = width*radio;
    height = height*radio;
    
    int xPos = (size.width - width)/2;
    int yPos = (size.height-height)/2;
    
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(xPos, yPos, width, height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
}

这两个方法的结合使用应该注意的是rect和size中宽高比要保持一致,这样展示的图片才不会有问题。应该只有宽高比一致,那么- (UIImage*)scaleToSize:(CGSize)size方法中的xPos,yPos就都为0,缩放后的图片才会布满整个size,否则的话就会有留白。

如果我们要展示原图的缩略图(不需要剪切),我们也可以交换两个方法的使用,同样的要保持rect和size中宽高比要保持一致。

本文非原创,来源网络资源与现实项目需求的整合。

相关文章

  • 图片简单处理

    均属于笔记,仅供个人参考,有问题欢迎指正 public class BitmapUtil { //@# 图片加载工...

  • 简单处理图片

    图片格式 置灰:(人眼感知)float gray = 0.3 * color.r+0.59 *color.g+0...

  • 图片的简单处理

    需求:很多时候我们需要对拍照后的图片进行处理展示或者上传,但是原图的数据是很大,或者我们要展示的区域很小,这时我们...

  • Bootstrap相关

    1.Bootstrap 样式之 响应式图片的处理 响应式图片处理 在bootstrap中如何使用响应式图片? 简单...

  • oss 图片处理

    图片处理介绍: 图片处理文章链接 使用: 原始图片上传到OSS后,可以通过简单的RESTful接口,在任何时间、任...

  • 图片批量处理格式大小加水印重命名全能工具

    图片批处理王是一款完全免费且功能强大的图片批处理软件,帮助用户全面简单实用的处理大量图片,提高工作效率。拥有图片旋...

  • 简单的图片+标题样式处理

    效果类似于贴吧的 精 图片+标题,像下面这样 一个图片+文字,或者复杂点的格式。。。iOS7之前,要做这样的效果需...

  • PIL-图片简单处理

    最近工作中有一些简单的图片处理的需求,记录一下学习过程。 一些概念 先了解一些概念 光栅图像 光栅图像(raste...

  • 图片简单处理(尺寸变换)

    图片拉伸和尺寸变换 图片拉伸 图片的处理大概分 截图(capture), 缩放(scale), 设定大小(res...

  • 用python简单处理图片

    一:打开显示保存 二.图像通道几何变换裁剪 三. 添加水印 四.图像中的像素访问 五.图像直方图 (第一部分说明)...

网友评论

      本文标题:图片的简单处理

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