获取相机滚动iOS的某些视图的屏幕截图

时间:2013-08-02 06:28:53

标签: ios objective-c

您好我仍然是Objective-C的初学者,我希望有人可以帮助我。我以编程方式创建的是UIScrollView,其中包含可以滑动的图像。然后我创建了一个UIButton,当按下时应该截取已选择的图像子视图的屏幕截图,然后将其保存到相机胶卷(该部分我不确定在代码中添加的位置)。我希望它是截图而不仅仅是图像的原因是因为我稍后会在图像上添加UILabels,我希望它们也会出现在保存的屏幕截图中。我不太确定在saveWallpaper方法中使用哪些视图,即使我当前的解决方案可行。我查看了这个答案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];
    }

}

1 个答案:

答案 0 :(得分:0)

而不是像以下语句中那样绘制完整的滚动条视图

[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];

使用drawInRect方法绘制选定的图像和文本。请参阅this相关帖子以供参考。