问题描述
|
大家好,我正在用下面的代码从上下文中获取png格式的图像并保存到特定路径中。
- (void)drawRect:(CGRect)rect {
UIGraphicsBeginImageContext(self.bounds.size);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(),0.0,1.0,0.2);
for(id rects in textRects){
CGRect rect = [rects CGRectValue];
CGContextFillRect(UIGraphicsGetCurrentContext(),rect);
}
CGContextstrokePath(UIGraphicsGetCurrentContext());
textimage = UIGraphicsGetimageFromCurrentimageContext();
UIGraphicsEndImageContext();
NSData *imageData=UIImagePNGRepresentation(textimage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
Nsstring *documentsDirectory = [paths objectAtIndex:0];
Nsstring *path=[documentsDirectory stringByAppendingPathComponent:@\"folder\"];
[imageData writetoFile:[path stringByAppendingPathComponent:@\"/subfolder/assets/0.png\"] atomically:YES];
}
这是我的问题是从上述代码获取图像后,我的视图颜色从通常可用的颜色(在创建png之前)变为全黑,任何人都可以给我确切原因的原因
提前致谢。解决方法
请参阅第4行代码从Context获取png图像。
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
谢谢