UIGraphicsBeginImageContext系列知识
2016-01-25 14:47
501 查看
UiImage处理
1、等比缩放
<pre name="code" class="objc"><span style="font-size:14px;"></span><pre name="code" class="objc" style="color: rgb(73, 73, 73); font-size: 14px; line-height: 21px;"><pre name="code" class="objc">- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
<span> </span>UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
<span> </span>[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
<span> </span>UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
<span> </span>UIGraphicsEndImageContext();
<span> </span>return scaledImage;
}
2、自定义大小
<span style="font-size:14px;"></span><pre name="code" class="objc" style="color: rgb(73, 73, 73); font-size: 14px; line-height: 21px;"><pre name="code" class="objc"><span style="font-size:14px;">- (UIImage *) reSizeImage:(UIImage *)image toSize:(CGSize)reSize {
<span> </span>UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
<span> </span>[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
<span> </span>UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
<span> </span>UIGraphicsEndImageContext();
<span> </span>return reSizeImage;
}</span>
3、处理某个特定 view
只要是继承UIView的object 都可以处理必须先import
QuzrtzCore.framework
-(UIImage*) captureView:(UIView *)theView {
<span style="white-space:pre"> </span>CGRect rect = theView.frame;
<span style="white-space:pre"> </span>UIGraphicsBeginImageContext(rect.size);
<span style="white-space:pre"> </span>CGContextRef context = UIGraphicsGetCurrentContext();
<span style="white-space:pre"> </span>[theView.layer renderInContext:context];
<span style="white-space:pre"> </span>UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
<span style="white-space:pre"> </span>UIGraphicsEndImageContext();
<span style="white-space:pre"> </span>return img;
}
4、存储图片
4.1、存储到app的文件里
4.2、存储到手机图片库中
<span style="font-family:SimSun;">CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);</span>
获取当前app的名称和版本号
<span style="font-family:SimSun;font-size:12px;">NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// app名称
NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
// app版本
NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
// app build版本
NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];</span>
1、等比缩放
<pre name="code" class="objc"><span style="font-size:14px;"></span><pre name="code" class="objc" style="color: rgb(73, 73, 73); font-size: 14px; line-height: 21px;"><pre name="code" class="objc">- (UIImage *) scaleImage:(UIImage *)image toScale:(float)scaleSize {
<span> </span>UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize);
<span> </span>[image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
<span> </span>UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
<span> </span>UIGraphicsEndImageContext();
<span> </span>return scaledImage;
}
2、自定义大小
<span style="font-size:14px;"></span><pre name="code" class="objc" style="color: rgb(73, 73, 73); font-size: 14px; line-height: 21px;"><pre name="code" class="objc"><span style="font-size:14px;">- (UIImage *) reSizeImage:(UIImage *)image toSize:(CGSize)reSize {
<span> </span>UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
<span> </span>[image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
<span> </span>UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
<span> </span>UIGraphicsEndImageContext();
<span> </span>return reSizeImage;
}</span>
3、处理某个特定 view
只要是继承UIView的object 都可以处理必须先import
QuzrtzCore.framework
-(UIImage*) captureView:(UIView *)theView {
<span style="white-space:pre"> </span>CGRect rect = theView.frame;
<span style="white-space:pre"> </span>UIGraphicsBeginImageContext(rect.size);
<span style="white-space:pre"> </span>CGContextRef context = UIGraphicsGetCurrentContext();
<span style="white-space:pre"> </span>[theView.layer renderInContext:context];
<span style="white-space:pre"> </span>UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
<span style="white-space:pre"> </span>UIGraphicsEndImageContext();
<span style="white-space:pre"> </span>return img;
}
4、存储图片
4.1、存储到app的文件里
<span style="font-family:SimSun;">NSString *path = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"image.png"]; [UIImagePNGRepresentation(image) writeToFile:pathatomically:YES];</span>
4.2、存储到手机图片库中
<span style="font-family:SimSun;">CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);</span>
获取当前app的名称和版本号
<span style="font-family:SimSun;font-size:12px;">NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// app名称
NSString *name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
// app版本
NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
// app build版本
NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"];</span>
相关文章推荐
- IOS高级教程1:处理1000张图片的内存优化
- 获取UIImage中的图像数据
- CALayer与UIView的关系
- UIView的bounds和frame区别
- UIImage的一个实例函数stretchableImageWithLeftCapWidth
- uiview
- uiview生命周期
- UIView翻译 (参考)
- iOS 转变UIView到UIImage
- UIView动画
- initWithFrame 和 initWithCoder
- [CS193P] 第六堂課摘要及心得筆記
- 【无线互联】基于ios系统的图片(滤镜)编辑
- UI第一天
- 棋盘
- ios实现button变换颜色并可以放大、缩小、旋转
- UIImageView的使用
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
- uiview contentViewmode
- iphone开发小记 基础知识2