Reputation: 855
I am trying to create a UIImageView from a CGContext but - for some reason- the image is slightly off target on my iPad version (but not iPhone!). I am passing in the textview itself (tried passing in the UIView layer as well...). I have included a picture of what viewImage looks like. Obviously, I'd like to have the image consist of the entire square with "See your flipped text here (when done)" perfectly enclosed...
Any ideas?!
Here is my code:
-(UIImage *)getImage:(UITextView *)v
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions([v frame].size, NO, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext([v frame].size);
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, -v.frame.origin.x, -v.frame.origin.y);
CGContextSaveGState(context);
[[self.view layer] renderInContext:context];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
This is the UIImage that is rendered from the context (the x,y coordinates of the frame origin seem fine....)
Could something be wrong with the View Controller? I don't think my settings are abnormal...
This is what I'd like the return UIImage to look like (more or less):
Upvotes: 1
Views: 134
Reputation: 534
It seems as though your view v is embedded within another view--in that case you are taking a snapshot from the wrong layer. 2 options: 1. Change the view whose layer you are taking the snapshot from to be v's superview (parent). 2. Move the view v outside, so that its parent view is self.view.
Cheers!
Upvotes: 1