`
lizhuang
  • 浏览: 900238 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

UIImage 图片处理:截图,缩放,设定大小,存储

阅读更多
原文:http://blog.csdn.net/xuhuan_wh/article/details/6434055

图片的处理大概分 截图(capture),  缩放(scale), 设定大小(resize),  存储(save)

1.等比率缩放
- (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
只要是继承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的文件里和储存到手机的图片库里

1) 储存到app的文件里
NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
把要处理的图片, 以image.png名称存到app home下的Documents目录里

2)储存到手机的图片库里(必须在真机使用,模拟器无法使用)
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
UIGetScreenImage(); // 原来是private(私有)api, 用来截取整个画面,不过SDK 4.0后apple就开放了

//====================================================================================

以下代码用到了Quartz Framework 和 Core Graphics Framework. 在workspace的framework目录里添加这两个framework.在UIKit里,图像类UIImage和CGImageRef的画图操作都是通过Graphics Context来完成。Graphics Context封装了变换的参数,使得在不同的坐标系里操作图像非常方便。缺点就是,获取图像的数据不是那么方便。下面会给出获取数据区的代码。



1. 从UIView中获取图像相当于窗口截屏。

(ios提供全局的全屏截屏函数UIGetScreenView(). 如果需要特定区域的图像,可以crop一下)

CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
2. 对于特定UIView的截屏。

(可以把当前View的layer,输出到一个ImageContext中,然后利用这个ImageContext得到UIImage)

-(UIImage*)captureView: (UIView *)theView
{
CGRect rect = theView.frame;
UIGraphicsBeginImageContext(rect.size);
CGContextRef context =UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return img;
}
3. 如果需要裁剪指定区域。

(可以path & clip,以下例子是建一个200x200的图像上下文,再截取出左上角)

UIGraphicsBeginImageContext(CGMakeSize(200,200));
CGContextRefcontext=UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
// ...把图写到context中,省略[indent]CGContextBeginPath();
CGContextAddRect(CGMakeRect(0,0,100,100));
CGContextClosePath();[/indent]CGContextDrawPath();
CGContextFlush(); // 强制执行上面定义的操作
UIImage* image = UIGraphicGetImageFromCurrentImageContext();
UIGraphicsPopContext();
4. 存储图像。

(分别存储到home目录文件和图片库文件。)

存储到目录文件是这样

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];
若要存储到图片库里面

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

5.  互相转换UImage和CGImage。

(UImage封装了CGImage, 互相转换很容易)

UIImage* imUI=nil;
CGImageRef imCG=nil;
imUI = [UIImage initWithCGImage:imCG];
imCG = imUI.CGImage;
6. 从CGImage上获取图像数据区。

(在apple dev上有QA, 不过好像还不支持ios)


下面给出一个在ios上反色的例子

-(id)invertContrast:(UIImage*)img
{
CGImageRef inImage = img.CGImage;
CGContextRef ctx;
CFDataRef m_DataRef;
m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));

int width = CGImageGetWidth( inImage );
int height = CGImageGetHeight( inImage );

int bpc = CGImageGetBitsPerComponent(inImage);
int bpp = CGImageGetBitsPerPixel(inImage);
int bpl = CGImageGetBytesPerRow(inImage);

UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);
int length = CFDataGetLength(m_DataRef);

NSLog(@"len %d", length);
NSLog(@"width=%d, height=%d", width, height);
NSLog(@"1=%d, 2=%d, 3=%d", bpc, bpp,bpl);

for (int index = 0; index < length; index += 4)
{
m_PixelBuf[index + 0] = 255 - m_PixelBuf[index + 0];// b
m_PixelBuf[index + 1] = 255 - m_PixelBuf[index + 1];// g
m_PixelBuf[index + 2] = 255 - m_PixelBuf[index + 2];// r
}

ctx = CGBitmapContextCreate(m_PixelBuf, width, height, bpb, bpl, CGImageGetColorSpace( inImage ), kCGImageAlphaPremultipliedFirst );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* rawImage = [UIImage imageWithCGImage:imageRef];
CGContextRelease(ctx);
return rawImage;
}


7. 显示图像数据区。

(显示图像数据区,也就是unsigned char*转为graphics context或者UIImage或和CGImageRef)

CGContextRef ctx = CGBitmapContextCreate(pixelBuf,width,height, bitsPerComponent,bypesPerLine, colorSpace,kCGImageAlphaPremultipliedLast );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage* image = [UIImage imageWithCGImage:imageRef];
NSString* path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ss.png"];
[UIImagePNGRepresentation(self.image) writeToFile:path atomically:YES];
CGContextRelease(ctx);
得到图像数据区后就可以很方便的实现图像处理的算法。
完,欢迎指正!
分享到:
评论

相关推荐

    UIImage 图片处理:截图,缩放,设定大小,存储_蓝科教育

    UIImage 图片处理:截图、缩放、设定大小、存储 UI 图片处理是 iOS 开发中一个非常重要的topic,UIImage 是 iOS 中表示图像的类,它提供了多种方法来处理图像,在这里我们将介绍 UIImage 图片处理的四种方法:截图...

    iphone 图像处理小结

    在iOS开发中,图像处理是不...以上是iPhone图像处理的一些基础操作,包括图片缩放、存储、加载、保存到相册以及在图片上绘制文字等。在实际开发中,开发者可能还需要考虑更多细节,如性能优化、内存管理、错误处理等。

    IOS应用源码——图片展示.rar

    10. **第三方库**: 如AlamofireImage、PINRemoteImage等,提供了丰富的功能,包括自动缓存、图片处理、占位符等,简化了图片加载流程。 这个源码示例很可能是展示如何结合上述技术来实现一个图片展示功能,可能包含...

    多线程SDWebImage.zip

    4. **图片处理**:支持图片格式转换、缩放、裁剪等操作。 5. **进度回调**:提供下载进度回调,可以展示下载进度条或者更新进度指示器。 6. **取消加载**:可以随时取消正在进行的图片加载任务。 7. **错误处理**:...

    ios图片上传服务器前的压缩处理

    然而,未经过处理的高清图片往往体积较大,如果直接上传至服务器,可能会消耗大量的网络带宽,增加服务器存储负担,甚至可能导致上传失败,特别是当服务器对上传文件大小有限制时。因此,在图片上传至服务器之前进行...

    从相册或拍照选择剪切 后的图片

    Android的BitmapOptions可以用来设定缩放比例,而iOS的UIImageJPEGRepresentation或UIImagePNGRepresentation可以将图片转换为二进制数据并调整质量进行压缩。 6. UI反馈:在用户进行操作时,提供适当的进度提示和...

    SDWebImage类库

    - **图片处理**:可自定义图片加载后的处理逻辑,如缩放、裁剪等。 - **占位符**:在图片下载期间显示占位符图片。 - **错误处理**:提供错误回调,便于处理加载失败的情况。 - **URL重试策略**:支持在网络问题...

    图片轮流切换显示

    1. 图片数组:存储要显示的图片URL或UIImage对象。 2. 自动滚动间隔:设定每次自动切换的时间间隔。 3. 缓动效果:设置滚动动画的缓动效果,使滚动更平滑。 4. 指示器:可选地,添加一个UIPageControl来指示当前显示...

    仿微信图片裁剪

    大尺寸图片处理可能导致内存问题,因此在实际应用中,通常需要先对图片进行按需缩放,减少处理的像素数量。此外,利用硬件加速和异步处理也是提高裁剪性能的有效手段。 6. **第三方库** 开发者也可以选择使用现成...

    调取相册和相机

    在这个过程中,涉及到的主要知识点包括Android或iOS平台的权限管理、图片选择器的实现、图片处理(如压缩)以及内存管理。 1. **权限管理**:在Android 6.0及以上版本,系统引入了运行时权限的概念,调用相册和相机...

    SDWebImage-translateTo-Chinese:经典图片缓存框架SDWebImage注释翻译

    4. **图片处理**: SDWebImage支持对下载的图片进行处理,如缩放、裁剪等,通过`transformDownloadedImage:imageWithUrl:`方法可以自定义处理逻辑,比如使用`UIImage+Resize`分类来调整图片尺寸。 5. **占位图与...

    UIImageViewAnimator 多媒体源码_ios源码

    在初始化一个UIImageViewAnimator实例时,开发者可以传入一个UIImage数组和对应的duration参数,用于设定每张图片显示的时间。这样,UIImageViewAnimator就能根据这些参数自动创建一个连续播放的动画效果。此外,它...

    图片裁剪和布局案例

    在iOS开发中,图片处理是一项常见的任务,尤其在设计用户界面和实现图像编辑功能时。本案例"图片裁剪和布局案例"由作者sanzhong538提供,源码名为SZChoosePicture,旨在展示如何在iOS应用中实现图片裁剪、布局计算...

    iOS实现动态元素的引导图效果

    - `LMJAnimatedElement`初始化时需要传入一个`UIImage`对象,这是元素的图片。 - `belongToScreen`属性用于设置元素属于哪个屏幕,便于管理和动画过渡。 - `size`属性定义了元素的尺寸,例如 `(64, 64)`。 - `...

Global site tag (gtag.js) - Google Analytics