iOS-图片处理的一些工具方法

271 篇文章 0 订阅
65 篇文章 0 订阅

图片拉伸和尺寸变换


图片拉伸 :

?
1
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

一般 leftCapWidth = imageWidth *.5f, topCapHeight = imageWidth * .5f;

?
1
2
3
4
5
6
7
8
9
尺寸变换:
//resize图片
- (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;
}


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


1.等比率缩放

?
1
2
3
4
5
6
7
- (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.自定长宽

?
1
2
3
4
5
6
7
- (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

?
1
2
3
4
5
6
7
8
9
-(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
2
3
4
//1) 储存到app的文件里
     NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@ "Documents" ]stringByAppendingPathComponent:@ "image.png" ];
     [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];
//把要处理的图片, 以image.png名称存到app home下的Documents目录里
?
1
2
3
4
5
//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一下)

?
1
2
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];


2. 对于特定UIView的截屏。

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

?
1
2
3
4
5
6
7
8
9
10
-(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的图像上下文,再截取出左上角)

?
1
2
3
4
5
6
7
8
9
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目录文件和图片库文件。)

存储到目录文件是这样

?
1
2
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@ "Documents" ] stringByAppendingPathComponent:@ "image.png" ];
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];


若要存储到图片库里面

?
1
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);


5.  互相转换UImage和CGImage。

?
1
2
3
4
5
//(UImage封装了CGImage, 互相转换很容易)
UIImage* imUI=nil;
CGImageRef imCG=nil;
imUI = [UIImage initWithCGImage:imCG];
imCG = imUI.CGImage;


6. 从CGImage上获取图像数据区。

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-(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. 显示图像数据区。

?
1
2
3
4
5
6
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);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值