2011-10-28 21 views

回答

2

這個代碼片斷顯示瞭如何渲染視圖到UIImage

UIGraphicsBeginImageContext(myView.bounds.size); 
[myView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

而這個片段展示瞭如何保存UIImage爲JPEG或PNG:

NSString *pngPath = [NSHomeDirectory() 
    stringByAppendingPathComponent:@"Documents/Test.png"]; 
NSString *jpgPath = [NSHomeDirectory() 
    stringByAppendingPathComponent:@"Documents/Test.jpg"]; 
[UIImageJPEGRepresentation(viewImage, 1.0) writeToFile:jpgPath atomically:YES]; 
[UIImagePNGRepresentation(viewImage) writeToFile:pngPath atomically:YES]; 
相關問題