将UIView转成UIImage,将UIImage转成PNG/JPG
2013-03-12 17:37
120 查看
//UIView -> UIImage
#import “QuartzCore/QuartzCore.h”
//把UIView 转换成图片
-(UIImage *)getImageFromView:(UIView *)view{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//UIImage -> PNG / JPG
// Create paths to output images
NSString
*pngPath
=
[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents/Test.png"];
NSString
*jpgPath
=
[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image,
1.0) writeToFile:jpgPath
atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image)
writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// Create file manager
NSError
*error;
NSFileManager
*fileMgr
=
[NSFileManager defaultManager];
// Point to Document directory
NSString
*documentsDirectory
=
[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents
directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory
error:&error]);
http://blog.163.com/lzb4319@126/blog/static/7255470020125693048341/
#import “QuartzCore/QuartzCore.h”
//把UIView 转换成图片
-(UIImage *)getImageFromView:(UIView *)view{
UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//UIImage -> PNG / JPG
// Create paths to output images
NSString
*pngPath
=
[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents/Test.png"];
NSString
*jpgPath
=
[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents/Test.jpg"];
// Write a UIImage to JPEG with minimum compression (best quality)
// The value 'image' must be a UIImage object
// The value '1.0' represents image compression quality as value from 0.0 to 1.0
[UIImageJPEGRepresentation(image,
1.0) writeToFile:jpgPath
atomically:YES];
// Write image to PNG
[UIImagePNGRepresentation(image)
writeToFile:pngPath atomically:YES];
// Let's check to see if files were successfully written...
// Create file manager
NSError
*error;
NSFileManager
*fileMgr
=
[NSFileManager defaultManager];
// Point to Document directory
NSString
*documentsDirectory
=
[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
// Write out the contents of home directory to console
NSLog(@"Documents
directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory
error:&error]);
http://blog.163.com/lzb4319@126/blog/static/7255470020125693048341/
相关文章推荐
- 将UIView转成UIImage,将UIImage转成PNG/JPG
- 将UIView转成UIImage,将UIImage转成PNG/JPG
- 怎样将UIImage对象保存到JPG或者PNG文件中?
- 怎样将UIImage对象保存到JPG或者PNG文件中?
- iphone 怎样将UIImage对象保存到JPG或者PNG文件中?
- iOS_UIImage_jpg<-->png转换
- 将UIImage对象保存到JPG或者PNG文件中
- UIImage保存成png和jpg
- 加载图片 最好不要用myImage = [UIImage imageNamed:@"icon.png"]
- css考核点整理(十三)-jpg/png/gif等图片类型区别
- How to realize the UIView convert to UIImage?
- UIImageJPEGRepresentation和UIImagePNGRepresentation
- GIF, JPG and PNG – What’s the difference?
- png转jpg背景颜色改变的问题
- Linux下批量修改文件名的后缀 例如 .png文件修改为.jpg文件
- Linux下批量修改文件名的后缀 例如 .png文件修改为.jpg文件
- developer应该知道的image知识(JPG和PNG)
- Gif,png,jpg,webp几种图片格式整理
- js验证上传的文件是否为JPEG,PNG,JPG,GIF格式
- png,或jpg等等图片格式和webp图片格式互相转换的在线工具