1
我需要通過組合一些視覺元素來形成圖像並將其保存在磁盤上。問題是:如何「截圖」視圖的某個區域?可能是一個不可見的視圖,所以程序可以被忽視執行?如何從iOS上的GUI元素組成圖像?
我需要通過組合一些視覺元素來形成圖像並將其保存在磁盤上。問題是:如何「截圖」視圖的某個區域?可能是一個不可見的視圖,所以程序可以被忽視執行?如何從iOS上的GUI元素組成圖像?
這個代碼片斷顯示瞭如何渲染視圖到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];