1、等比例缩放
- (UIImage *)scaleImage:(UIImage *)imagetoScale:(float)scaleSize
{UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize);[image drawInRect:CGRectMake(0, 0,image.size.width * scaleSize, image.size.height *scaleSize)];UIImage *scaledImage =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return scaledImage;
}
2、自定长宽
- (UIImage *)reSizeImage:(UIImage *)imagetoSize:(CGSize)reSize
{UIGraphicsBeginImageContext(CGSizeMake(reSize.width,reSize.height));[image drawInRect:CGRectMake(0, 0, reSize.width,reSize.height)];UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return reSizeImage;
}
3、处理某个特定的view
只要是继承UIView的object都可以处理,必须先 import QuzrtzCore.framework
-(UIImage*)captureView:(UIView *)theView
{CGRect rect = theView.frame;UIGraphicsBeginImageContext(rect.size);CGContextRef context =UIGraphicsGetCurrentContext();[theView.layer renderInContext:context];UIImage *img =UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();return img;
}
4、存储图片
储存图片这里我分储存到APP的文件里,存储到手机图库里
储存到APP:
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
这样就把你要处理的图片,以image.image这个档名存储到APP home底下的documents目录里。
存储到手机图库:
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
UIGetScreenImage()原本是private(私有)api, 用来截取整个画面
不过SDK 4.0后apple就开放了
另外存存到手机图片库里, 必须在手机使用, 模拟器无法使用