gpt4 book ai didi

ios - 获取某些 View 的屏幕截图到相机胶卷 iOS

转载 作者:行者123 更新时间:2023-12-01 16:48:38 25 4
gpt4 key购买 nike

嗨,我仍然是 Objective-C 的初学者,希望有人能帮助我。我以编程方式创建的是 UIScrollView里面有你可以滑动的图像。然后我创建了一个 UIButton按下时应该截取已选择的图像 subview 的屏幕截图,然后将其保存到相机胶卷(那部分我不确定在代码中添加的位置)。我希望它是屏幕截图而不仅仅是图像的原因是因为我稍后会添加 UILabels在图像的顶部,我希望它们也出现在保存的屏幕截图中。我不太确定在 saveWallpaper 方法中使用哪些 View ,即使我当前的解决方案是否有效。我看过这个答案Link获取屏幕截图代码以尝试此实现。基本上我希望它截取除 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];
}

}

最佳答案

而不是像下面的语句那样绘制完整的 ScrollView

[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];

使用 drawInRect 方法绘制选定的图像和文本。见 this相关帖子供引用。

关于ios - 获取某些 View 的屏幕截图到相机胶卷 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18010168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com