开发者

CGRect to UIImage

开发者 https://www.devze.com 2023-04-08 20:25 出处:网络
I have this code: CGRect visibleRect; visibleRect.origin = mainViewController.scrollView.contentOffset;

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

精彩评论

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

关注公众号