ios图像处理

作者QQ:415074476

QQ群:191280586

图像处理

一.获取图片
a.摄像头
b.本地图片
c.网络图片
d.应用程序内截图
e.从头创建图片

二.图片处理
a. 缩放
b.旋转
c.质量

一.获取图片
a.摄像头
使用UIImagePickerController类拍打照或从相册获取图片
使用注意事项:使用之前一定要先做检测
isSourceTypeAvailable

b.本地图片
分应用程序包和沙盒.
应用程序包中的图片是开发阶段放入的.
获取方法 分两种:
第一种最简单:
myImage = [UIImage imageNamed:@”icon.png”];
第二种:
NSString *path =[[NSBundle mainBundle] pathForResource:@”icon ” ofType:@”png”];
myImage = [UIImage imageWithContentsOfFile:path];
第一种使用简单,但是会缓存下来,浪费内存.反复使用的图片建议使用第一种方式,这样效率高..只使用一两次,建议使用第二种,节省内存.

沙盒是程序运行过程中,存储下来的图片.
默认情况下, 沙盒有三个文件夹: Documents, Library, tmp
Documents存放生成的用户数据和图片等
Library 存放用户默认设置和其它状态信息.
tmp存放临时文件.临时文件一定得放在tmp目录下,否则上架审核通不过.

两种方式:
一.通用方式
NSArray  *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths lastObject];

二专用方法
return [NSHomeDirectory() stringByAppendingPathComponent:@”Documents”];

获取路径之后,使用UIImage的imageWithContentsOfFile方法生成图片.

c.网络图片
UIImage类可以从NSData实例加载图像.
最简单的办法

myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlstring]]];
对应的方法,把UIImage对象转成NSData对象
UIImagePNGRepresentation

由于上述方法是同步的,会阻塞用户交互,更好的办法是使用
NSURLConnection来异步获取图片数据.

d.应用程序内截图

1.把view转换成图片
其实就是使用CALayer的renderInContext方法.

+(UIImage *)imageFromView: (UIView *) theView
{

    UIGraphicsBeginImageContext(theView.frame.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    [theView.layer renderInContext:context];//key point

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicEndImageContext();

    return theImage;

}

e.从头创建图片
使用Quartz 2D创建图形
适用于创建一些简单的标签图形.
几个概念:
1.图形上下文
2.绘图状态,如填充颜色,笔触宽度等
3.绘制路径

二.图片处理
a.缩放

UIGraphicsBeginImageContext(viewsize);
float dwidth = (viewsize.width - size.width) / 2.0f;
float dheight = (viewsize.height - size.height) / 2.0f;
CGRect rect = CGRectMake(dwidth, dheight, size.width, size.height);
[image drawInRect:rect];//key point
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

b.旋转

仅用于视图显示时可简单地用动画属性
transform.rotation.z
如果要处理图片本身就要使用core graphics了.
UIImage 对象有个属性为imageOrientation.
8个值.上下左右,及在上下左右的基础上加的镜像.
代码如下:

    CGImageRef imgRef = aImage.CGImage;
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    CGFloat scaleRatio = 1;
    CGFloat boundHeight;

UIImageOrientation orient = aImage.imageOrientation;
switch(orient)
{
case UIImageOrientationUp: //EXIF = 1
{
transform = CGAffineTransformIdentity;
break;
}
case UIImageOrientationUpMirrored: //EXIF = 2
{
transform = CGAffineTransformMakeTranslation(width, 0.0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
break;
}
case UIImageOrientationDown: //EXIF = 3
{
transform = CGAffineTransformMakeTranslation(width, height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
}
case UIImageOrientationDownMirrored: //EXIF = 4
{
transform = CGAffineTransformMakeTranslation(0.0, height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
break;
}
case UIImageOrientationLeftMirrored: //EXIF = 5
{
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(height, width);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
}
case UIImageOrientationLeft: //EXIF = 6
{
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(0.0, width);
transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
break;
}
case UIImageOrientationRightMirrored: //EXIF = 7
{
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeScale(-1.0, 1.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
}
case UIImageOrientationRight: //EXIF = 8
{
boundHeight = bounds.size.height;
bounds.size.height = bounds.size.width;
bounds.size.width = boundHeight;
transform = CGAffineTransformMakeTranslation(height, 0.0);
transform = CGAffineTransformRotate(transform, M_PI / 2.0);
break;
}
default:
{   DLOG(@"Invalid image orientation");
return aImage;
}
}

UIGraphicsBeginImageContext(bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
CGContextScaleCTM(context, -scaleRatio, scaleRatio);
CGContextTranslateCTM(context, -height, 0);
}
else {
CGContextScaleCTM(context, scaleRatio, -scaleRatio);
CGContextTranslateCTM(context, 0, -height);
}

c.降低质量

UIKIT_EXTERN NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);  // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)