0

I made a 3x3 cell layout with UIViews. So there are 9 UIViews!!!

I am trying to screenshot these 9 UIviews individually. But Whatever I tried, I can only screenshot the 1st UIview.

HEre is the screenshot of the main view that holds the 9 subviews:

enter image description here

And I want to screenshot the second tile by sending _tile2, but the final result I get is _tile1 only.

:enter image description here

Here is the code:

     [self saveImage:[self screenshotTile:_tile2]]; //_tile1,_tile2...._tile9 gave the same result

What I believe is If I send the _tile1, it should screenshot the _tile1 area on the self.view, and If I send the _tile2, it should screenshot the _tile2 area on the self.view .But whatever I send, it only captures the _tile1 area on the view.

-(void)saveImage:(UIImage *)img{

    UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}

-(UIImage *)screenshotTile :(UIView *)imgV{

    UIGraphicsBeginImageContext(imgV.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    _tileImg1 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return _tileImg1;
 }

I tried this solution which suggests to shift the image after taking the screenshot, but it didnt work too:

     UIGraphicsBeginImageContext(sshot.frame.size);
     [sourceImage drawAtPoint:CGPointMake(-50, -100)]; // I tried -imgV.frame.origin.v, -imgV.frame.origin.y but it didnt work for me!!!
Community
  • 1
  • 1
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109

2 Answers2

0

You pass a tile to the screenshotTile: method. In that method, you never actually use the tile. Maybe you want to send renderInContext: to imgV.layer, not to self.view.layer.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • This is not the case I need rob!!! If I send imgV.layer I will get the screenshot of the tile!!!! But I need the screenshot of the self.view.layer under the tile!!!! As you can see, there are 9 colored tiles, If I screenshot the tile, I get just the color tile as image. But I need the image under the tile which is self.view.layer!!!! – Teja Nandamuri Nov 10 '15 at 13:05
  • I am sending the tile to screenshot method because, I need the screenshot of tile frame and tile size on the self.view.layer – Teja Nandamuri Nov 10 '15 at 13:08
0

After few trials, I managed to get it worked by using this code:

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(c, -imgV.frame.origin.x, -imgV.frame.origin.y);

[self.view.layer renderInContext:c];

I need to shift the context x axis and y axis according to the tile frame. The context always starts from the 0,0 point, So I need to move the image according to the tile frame.

In the process of doing this, I encountered one wierd error which stopped me in saving the images to the camera roll:

Connection to assetsd was interrupted or assetsd died

I am not sure why and how it occured. After few trials, the error disappeared. I dont even know how ti reproduce it!!!!!

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109