UIImage 图片处理
2014-04-22 18:42
218 查看
1.等比率缩放
2.自定义宽高
3.处理某个特定的view
其中,在view里面实现
4.存储图片,存到app的文件里
5.在viewdidload里测试
//等比率缩放 - (UIImage *)scaleImage:(UIImage *)image toScale:(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 *)image toSize:(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
//处理某个特定的view - (UIImage *)captureView:(UIView *)theView { //取得当前view的frame CGRect rect = theView.frame; //开始上下文 UIGraphicsBeginImageContext(rect.size); // CGContextRef context = UIGraphicsGetCurrentContext(); // [theView.layer renderInContext:context]; //将view转换为图片 UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); //结束上下文 UIGraphicsEndImageContext(); //返回图片 return img; }
其中,在view里面实现
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; imageView.image = [UIImage imageNamed:@"1.jpg"]; [self addSubview:imageView];
4.存储图片,存到app的文件里
- (void)saveImage:(UIImage *)image image:(int)location{ if (location == 1) { //存储路径,以及存储的文件名字 NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"xiaoye.png"]; //将图片写入文件 [UIImagePNGRepresentation(image) writeToFile:path atomically:YES]; //找到Documents文件目录 NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; //输出Documents目录下得文件名字 NSLog(@"存储到docments目录下的文件有:%@",[[NSFileManager defaultManager] subpathsAtPath:docPath]); } }
5.在viewdidload里测试
UIImage *image = [UIImage imageNamed:@"1.jpg"]; //等比率的缩放 UIImage *scaledImage = [self scaleImage:image toScale:0.5f]; NSLog(@"%@",scaledImage); //自定长宽 UIImage *reSizeImage = [self reSizeImage:image toSize:CGSizeMake(100.0f, 50.0f)]; NSLog(@"%@",reSizeImage); //处理特定的view,继承自uiview,需要导入QuzrtzCore.framework MyView *view = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 160, 240)]; UIImage *img = [self captureView:view]; NSLog(@"%@",img); //存储图片到Documents目录下 [self saveImage:image image:1]; //测试图片,scaledImage,reSizeImage,img self.view.backgroundColor = [UIColor colorWithPatternImage:img];
相关文章推荐
- iPhone处理图片(UIImage扩展类) 自动适应frame大小方法
- UIImage 图片处理:截图,缩放,设定大小,存储
- UIImage图片处理
- UIImage 图片处理:截图,缩放,设定大小,存储
- UIImage 图片处理:截图,缩放,设定大小,存储
- 可以利用这种方式实现图片UIImage翻转,本人用于自拍后,对左右相反照片的处理
- UIImage图片处理(更改颜色,修改透明度,缩小,合并图片)
- UIImage 图片处理:截图,缩放,设定大小,存储
- UIImage处理图片拉伸
- UIImage 图片处理:截图,缩放,设定大小,存储
- UIImageView处理, 图片处理
- UIImage图片处理
- UIImage 图片处理:合成,截图,缩放,设定大小,存储
- UIImage 图片处理:截图,缩放,设定大小,存储
- UIimage 图片的处理:缩放,设定大小,存储,截图
- UIImage图片处理:缩放、设定大小、存储 (转载)
- UIImage 图片处理:截图,缩放,设定大小,存储
- UIImage 图片处理:截图,缩放,设定大小,存储
- 基于SDWebImage的网络图片加载处理 & 获取网络中得UIimage
- UIImage 图片处理:截图,缩放,设定大小,存储