开发者

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]; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜