1

I need to crop an image to a rectangle with a height of 25 and a width the length of the screen.

I have the image ready and positioned in the center of the screen. I'd like to screenshot ONLY the rectangle.

I followed this answer, but it does not seem to be working for me for some reason.

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.width,25), false, 0) self.view.drawViewHierarchyInRect(CGRectMake(self.view.frame.width,25,view.bounds.size.width,view.bounds.size.height), afterScreenUpdates: true) let image:UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() statusImage = image 

1 Answer 1

2

You are using wrong coordinates. With your code, the drawing starts at the top right corner of the view and draws void space at the right side of it. You should use 0 as X value and a negative Y value. For example, if you want to screenshot a bar starting at Y=50, you should use:

self.view.drawViewHierarchyInRect(CGRectMake(0, -50, view.bounds.size.width, view.bounds.size.height), afterScreenUpdates: true) 

If you want to draw from the top of the view, just put 0 as Y value as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Tish what about my coordinate which is (8.0, 100.0, 360.0, 335.0) X,Y, width,height respectively .thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.