嗨,我仍然是 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];
}
}