CGRect to UIImage
I have this code:
CGRect visibleRect;
visibleRect.origin = mainViewController.scrollView.contentOffset;
visibleRect.size = mainViewController.scrollView.bounds.size;
I want to store the content inside visibleRect
in a UIImage
. Is that possible?开发者_Python百科
This code may work:
UIGraphicsBeginImageContext(visibleRect.size);
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), visibleRect.origin.x,
visibleRect.origin.y);
[mainViewController.scrollView.layer
renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
You can't store a UIImage in the CGRect. You'd do something like the following
CGRect visibleRect;
visibleRect.origin = mainViewController.scrollView.contentOffset;
visibleRect.size = mainViewController.scrollView.bounds.size;
UIImageView* img = [[UIImageView alloc] init];
[img setFrame:visibleRect];
[img setImage:[UIImage imageNamed:@"Your-img"]];
[[self view] addSubview:img];
[img release];
精彩评论