UIView / UIImage 截图(capture), 缩放(scale), 设定大小(resize),储存(save)

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就开放了
另外存存到手机图片库里, 必须在手机使用, 模拟器无法使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值