您的位置:首页 > 产品设计 > UI/UE

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的文件里

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uiview uiimage