Iphone 有没有办法在不捕获整个屏幕的情况下将UIView保存到png
我试图做的是将接收用户输入的UIView保存为图形,并仅保存图形,不带背景,这样在应用程序中作为设置,用户可以更改他们正在绘制的背景图像 我已经找到了很多用于截屏的代码,但是没有关于保存一个UIView的代码 有什么建议吗?你可以看看,这正是你想要的。Iphone 有没有办法在不捕获整个屏幕的情况下将UIView保存到png,iphone,objective-c,ios,cocoa-touch,uiview,Iphone,Objective C,Ios,Cocoa Touch,Uiview,我试图做的是将接收用户输入的UIView保存为图形,并仅保存图形,不带背景,这样在应用程序中作为设置,用户可以更改他们正在绘制的背景图像 我已经找到了很多用于截屏的代码,但是没有关于保存一个UIView的代码 有什么建议吗?你可以看看,这正是你想要的。 CGSize imgsize = self.view.bounds; UIGraphicsBeginImageContext(imgsize); [self.view.layer renderInContext:UIGraphicsGetCurr
CGSize imgsize = self.view.bounds;
UIGraphicsBeginImageContext(imgsize);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *NewImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(NewImg,1);
NSString *fullPath = @""; //Path of Document directory where u wish to save the image.
BOOL success = [mediaData writeToFile:fullPath atomically:YES];
无论如何,这就是你需要的代码
UIView *view = ...;
CGSize size = [view bounds].size;
UIGraphicsBeginImageContext(size);
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这应仅捕获以下视图:
UIGraphicsBeginImageContext(someView.bounds.size);
[someView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这是
UIView
的类别:
@interface UIView(Image)
- (UIImage*)image;
@end
#导入“UIView+Image.h”
#进口
@实现UIView(图像)
-(UIImage*)图像{
CGSize imageSize=self.bounds.size;
UIGraphicsBeginImageContext(图像大小);
CGContextRef imageContext=UIGraphicsGetCurrentContext();
CgContextTranslateCm(imageContext,0.0,imageSize.height);
CGContextScaleCTM(imageContext,1.0,-1.0);
//for(在self.layer.sublayers中的CALayer*层){
//[图层渲染上下文:imageContext];
//}
[self.layer renderInContext:imageContext];
UIImage*viewImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
返回视图图像;
}
@结束
感谢您的快速回答。。。我会尝试一下,然后投票,然后接受第一个有效的。很好。我的意思是触摸可可。
#import "UIView+Image.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView(Image)
- (UIImage *)image {
CGSize imageSize = self.bounds.size;
UIGraphicsBeginImageContext(imageSize);
CGContextRef imageContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(imageContext, 0.0, imageSize.height);
CGContextScaleCTM(imageContext, 1.0, -1.0);
//for (CALayer* layer in self.layer.sublayers) {
// [layer renderInContext: imageContext];
//}
[self.layer renderInContext: imageContext];
UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
@end