免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4068 | 回复: 1
打印 上一主题 下一主题

iphone开发--UIImage 图片处理:截图,缩放,设定大小,存储 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-24 07:52 |只看该作者 |倒序浏览
iphone开发--UIImage 图片处理:截图,缩放,设定大小,存储






转自:http://my.oschina.net/rareliu/blog/24322



1、等比缩放



C代码  
- (
  1. UIImage *) scaleImage:(UIImage *)image toScale:(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. }  

  8. - (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
  9.     UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
  10.     [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
  11.     UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  12.     UIGraphicsEndImageContext();
  13.     return scaledImage;
  14. }
复制代码
2、自定义大小



C代码  
- (
  1. UIImage *) reSizeImage:(UIImage *)image toSize:(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.     return reSizeImage;   
  7. }  

  8. - (UIImage *) reSizeImage:(UIImage *)image toSize:(CGSize)reSize {
  9.     UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
  10.     [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
  11.     UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
  12.     UIGraphicsEndImageContext();
  13.     return reSizeImage;
  14. }
复制代码
3、处理某个特定的view

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



C代码
  1. -(UIImage*) captureView:(UIView *)theView {   
  2.     CGRect rect = theView.frame;   
  3.     UIGraphicsBeginImageContext(rect.size);   
  4.     CGContextRef context = UIGraphicsGetCurrentContext();   
  5.     [theView.layer renderInContext:context];   
  6.     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();   
  7.     UIGraphicsEndImageContext();   
  8.     return img;   
  9. }  

  10. -(UIImage*) captureView:(UIView *)theView {
  11.     CGRect rect = theView.frame;
  12.     UIGraphicsBeginImageContext(rect.size);
  13.     CGContextRef context = UIGraphicsGetCurrentContext();
  14.     [theView.layer renderInContext:context];
  15.     UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
  16.     UIGraphicsEndImageContext();
  17.     return img;
复制代码
}

4、存储图片

    4.1、存储到app的文件里

    把要处理的图片以image.png的名字存储到app home地下的Document目录中



C代码
  1. NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];   
  2. [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];  

  3. NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
  4. [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
复制代码
4.2、存储到手机的图片库中



C代码
  1. CGImageRef screen = UIGetScreenImage();   
  2. UIImage* image = [UIImage imageWithCGImage:screen];   
  3. CGImageRelease(screen);   
  4. UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);  

  5. CGImageRef screen = UIGetScreenImage();
  6. UIImage* image = [UIImage imageWithCGImage:screen];
  7. CGImageRelease(screen);
  8. UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
复制代码
有些没有实验,错误见谅~~

论坛徽章:
0
2 [报告]
发表于 2012-02-25 11:58 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP