从 renderInContext 中删除图层
我使用此代码制作屏幕截图,
CGFloat breed = 768;
CGFloat hoogte = 975;
CGSize afmeting = CGSizeMake(breed, hoogte);
UIGraphicsBeginImageContext(afmeting);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
图像中有一些我不想要的图层(按钮和文本标签)。 有没有办法从 renderincontext 中删除一些图层?
现在,我在制作屏幕截图之前使用 textLabel.hidden = YES,并在完成后将隐藏设置为 NO。也适用于按钮和其他标签。
I use this code to make a screenshot,
CGFloat breed = 768;
CGFloat hoogte = 975;
CGSize afmeting = CGSizeMake(breed, hoogte);
UIGraphicsBeginImageContext(afmeting);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
There are some layers (buttons and textlabels) that I don't want in the image.
Is there a way to remove some layers from renderincontext?
Now I use textLabel.hidden = YES before making the screenshot and set hidden to NO when finished. Also for the buttons and other labels.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有按钮和标签都有自己的层。所有这些层都可以渲染到某种图形上下文中。现在您正在生成 self.view.layer 作为图像,您可以尝试创建另一个视图,该视图仅包含您想要渲染的项目(将标签和按钮保持在另一个与“图片视图”重叠的视图中分开)或者您实际上可以一一渲染所有组件,不包括按钮(尽管这可能比您现在所做的工作更多)。如果您考虑将一个视图放在另一个视图之上,请记住,如果您希望顶部视图做出相应反应,则必须将顶部视图中的所有输入(触摸和事件)发送到底部视图。
All the buttons and labels have their own layers. All these layers can be rendered to some graphical context. Now you are generating your self.view.layer as image, you could try to make another view that would consist only of items you do want to render (keeping your labels and buttons separate in another view that would overlap your "picture view") or you can in fact render all components one by one not including your buttons (though that would probably be more work than you do now). If you will consider making one view on top of another, bear in mind that all the inputs (touches and events) in the top view must be sent to bottom view if you want it to react accordingly.