2

Hi I am still a beginner with Objective-C and am hoping someone can help me out. What I have created programmatically is a UIScrollView that has images inside that you can swipe across. Then I created a UIButton that when pressed is supposed to take a screenshot of the image subview the has been selected and then saves it to the camera roll (that part i'm not sure where to add in the code). The reason I want it to be a screenshot and not just the image is because I will later add UILabels on top of the images and I want those to appear in the saved screenshot as well. I am not quite sure which views to use in the saveWallpaper method and even if my current solution will work. I have looked at this answer Link to get the screenshot code to try for this implementation. Basically I want it to take a screenshot of everything besides the UIButton and save it to the camera roll. Any help or direction would be greatly appreciated. I could also try an approach where it takes a screenshot or captures certain elements and adds them together to be a single image. Thank you!

- (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];
    }

}
Community
  • 1
  • 1
Grant Wilkinson
  • 1,088
  • 1
  • 13
  • 38

1 Answers1

0

Rather than drawing the complete scroller view as in the following statement

[scroller.layer renderInContext:UIGraphicsGetCurrentContext()];

Draw the selected image and the text using drawInRect method. See this related post for reference.

Community
  • 1
  • 1
AnkitJain
  • 115
  • 5