Search code examples
graphicsxcode4screenshot

xcode: trying to capture a partial screen


I must be doing something wrong, but I could use some advice on the following:

I am trying to capture a partial screen on both iPad and iPad retina.

I can get the portrait views ok, but I cannot get the right side of a landscape view to be in bounds.

This is the code I'm using (I did not have this problem before UIGetScreenImage() was made contraband)..

CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
[[UIColor blackColor] set]; 
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


CGImageRef imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake (0, 100, self.view.bounds.size.width, (self.view.bounds.size.height - 200)));


UIImage *img2Save = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
// Request to save the image to camera roll
UIImageWriteToSavedPhotosAlbum(img2Save, self, nil, nil);

These are the two images I get for portrait, and landscape:

Portrait

Landscape


Solution

  • If you are using your window as your frame, it will always show up in portrait mode. You can set screenRect to a full-screen view, which should work in both portrait and landscape mode.