iOS 关于图片处理的一些方法。。。

第一篇博客,总结一下图片处理的一些方法,以供后续补充使用:

一、关于图片压缩:

在Iphone上有两种读取图片数据的简单方法:UIImageJPEGRepresentationUIImagePNGRepresentation

UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.

UIImagePNGRepresentation只需要图片引用作为参数.

通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小


二、关于图片的重新绘制:

1. 等比缩放:

  1. - (UIImage *) scaleImage:(UIImage *)imagetoScale:(float)scaleSize {
  2. UIGraphicsBeginImageContext(CGSizeMake(image.size.width *scaleSize, image.size.height * scaleSize);
  3. [image drawInRect:CGRectMake(0, 0, image.size.width *scaleSize, image.size.height * scaleSize)];
  4. UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext();
  5. UIGraphicsEndImageContext();
  6. return scaledImage;
  7. }

2. 自定义大小:

  1. - (UIImage *) reSizeImage:(UIImage *)imagetoSize:(CGSize)reSize {
  2. UIGraphicsBeginImageContext(CGSizeMake(reSize.width,reSize.height));
  3. [image drawInRect:CGRectMake(0, 0, reSize.width,reSize.height)];
  4. UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();
  5. UIGraphicsEndImageContext();
  6. returnreSizeImage;
  7. }
3. 获取某个特定的view:


   只要是继承UIView的object 都可以处理
   必须先import QuzrtzCore.framework

  1. -(UIImage*) captureView:(UIView *)theView{
  2. CGRect rect = theView.frame;
  3. // 创建一个bitmap的context  
        // 并把它设置成为当前正在使用的context 
  4. UIGraphicsBeginImageContext(rect.size);
  5. CGContextRef context =UIGraphicsGetCurrentContext();
  6. [theView.layer renderInContext:context];
  7. // 从当前context中创建一个改变大小后的图片
  8. UIImage *img =UIGraphicsGetImageFromCurrentImageContext();
  9. // 使当前的context出堆栈
  10. UIGraphicsEndImageContext();
  11. returnimg;
  12. }

4. 根据给定的图片裁剪生成新的图片

-(UIImage *)getImageFromImage{

//大图bigImage

//定义myImageRect,截图的区域

CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);

UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];

CGImageRef imageRef = bigImage.CGImage;

CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

 CGSize size;

size.width = 57.0;

size.height = 57.0;

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, myImageRect, subImageRef);

UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

UIGraphicsEndImageContext();

return smallImage;

}


下面补充一些在开发中写得几个方法,实用性稍微强一点的!主要功能实现从图片中心进行剪切!

//剪切图片(从中心算起)
- (UIImage *)getCutImageSize:(CGSize)size originalImage:(UIImage *)originalImage{
    originalImage = [self equalScaleCompressImage:originalImage size:size];
    CGImageRef imageRef = originalImage.CGImage;
    CGImageRef cutImageRef = CGImageCreateWithImageInRect(imageRef, [self getCutRectWithBigSize:originalImage.size cutRect:size]);
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, [self getCutRectWithBigSize:originalImage.size cutRect:size], cutImageRef);
    UIImage *cutImage = [UIImage imageWithCGImage:cutImageRef];
    UIGraphicsEndImageContext();
    
    return cutImage;
}
//获取截图区域(从中心算起)
- (CGRect)getCutRectWithBigSize:(CGSize)bigSize cutRect:(CGSize)cutSize{
    CGPoint bigPoint = CGPointMake(bigSize.width / 2.0f, bigSize.height / 2.0f);
    CGRect Rect = CGRectMake(bigPoint.x - cutSize.width / 2.0f, bigPoint.y - cutSize.height / 2.0f, cutSize.width, cutSize.height);
    return Rect;
}
//等比压缩图片
//如果自动适应image的size  以宽、高最大值为主
- (UIImage *)equalScaleCompressImage:(UIImage *)bigImage size:(CGSize)size{
    CGFloat scale = [self getCompressScaleWithBigSize:bigImage.size smallSize:size];
    UIGraphicsBeginImageContext(CGSizeMake(bigImage.size.width * scale, bigImage.size.height * scale));
    [bigImage drawInRect:CGRectMake(0, 0, bigImage.size.width * scale, bigImage.size.height * scale)];
    UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData *data = UIImageJPEGRepresentation(scaledImage, 1.0);
    [data writeToFile:@"/Users/tongkun/Desktop/123344" atomically:YES];
    return scaledImage;
}
//获取压缩比scale
- (CGFloat)getCompressScaleWithBigSize:(CGSize)bigSize smallSize:(CGSize)smallSize{
    CGFloat scale;
    if (bigSize.height / bigSize.width >= smallSize.height / smallSize.width) {
        scale = smallSize.width / bigSize.width;
    }else{
        scale = smallSize.height / bigSize.height;
    }
    return scale;
}





 

你可能感兴趣的:(iOS 关于图片处理的一些方法。。。)