![](/img/trans.png)
[英]iOS deve with Starling framework: take a screenshot and save it to the camera roll
[英]Getting Screenshot of certain views to camera roll iOS
嗨,我仍然是Objective-C的初学者,希望有人能帮助我。 我以编程方式创建的是一个UIScrollView
,其中包含可在其中滑动的图像。 然后,我创建了一个UIButton
,当按下该按钮时应该对图像子视图进行截图,然后将其选中,然后将其保存到相机胶卷中(该部分我不确定在代码中的何处添加)。 我希望它不仅是图像而是屏幕截图的原因是,稍后我将在图像之上添加UILabels
,并且希望这些图像也显示在保存的屏幕截图中。 我不确定在saveWallpaper方法中使用哪些视图,即使我当前的解决方案也可以使用。 我已经看过这个答案链接,以获取屏幕快照代码以尝试此实现。 基本上,我希望它拍摄除UIButton
之外的所有内容的屏幕快照,并将其保存到相机胶卷中。 任何帮助或指示将不胜感激。 我也可以尝试一种方法,该方法需要截屏或捕获某些元素,然后将它们添加在一起成为单个图像。 谢谢!
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 21;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:@"1.png",@"2.png",@"3.png",@"4.png",@"5.png",@"6.png",@"7.png",@"8.png",@"9.png",@"10.png",@"11.png",@"12.png",@"13.png",@"14.png",@"15.png",@"16.png",@"17.png",@"18.png",@"19.png",@"20.png",@"21.png", nil];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
scroller.pagingEnabled = YES;
scroller.bounces = NO;
[self.view addSubview:scroller];
int width=scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[scroller addSubview:ImgView];
scroller.contentSize = CGSizeMake(width, 0);
width +=scroller.frame.size.width;
xPos +=scroller.frame.size.width;
}
UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
[saveButton addTarget:self
action:@selector(saveWallpaper:)
forControlEvents:UIControlEventTouchDown];
[saveButton setImage:[UIImage imageNamed:@"save.png"] forState: UIControlStateNormal];
[saveButton setImage:[UIImage imageNamed:@"savepressed.png"] forState: UIControlStateHighlighted];
saveButton.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2, 396, 96.5, 42); //Make this in the center
[self.view addSubview:saveButton];
}
- (void)saveWallpaper:(id)sender
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(scroller.bounds.size, NO, [UIScreen mainScreen].scale);
}
else
{
UIGraphicsBeginImageContext(scroller.bounds.size);
[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];
}
}
而不是像下面的语句那样绘制完整的滚动视图
[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];
使用drawInRect方法绘制选定的图像和文本。 请参阅此相关文章以供参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.