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

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