IOS: CGRect to UIImage?

Up vote 1 down vote favorite share g+ share fb share tw.

I have this code: CGRect visibleRect; visibleRect. Origin = mainViewController.scrollView. ContentOffset; visibleRect.

Size = mainViewController.scrollView.bounds. Size; now I want to store in an UIImage the visibleRect, is it possible? Ios xcode uiscrollview uiimage link|improve this question asked Sep 28 '11 at 14:48blackguardian949740 89% accept rate.

This code may work: UIGraphicsBeginImageContext(visibleRect. Size); CGContextTranslateCTM(UIGraphicsGetCurrentContext(), visibleRect.origin. X, visibleRect.origin.

Y); mainViewController.scrollView. Layer renderInContext:UIGraphicsGetCurrentContext(); UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext().

I think it will still work - the compiler flakes out on layer methods, even though they're there and will work fine. – MusiGenesis Sep 28 '11 at 15:19 2 I add #import then it now work, but I have a white rect and not the content of scrollview – blackguardian Sep 28 '11 at 16:07 @blackguardian: thanks, you just fixed a warning I was getting from trying to call setMasksToBounds on a view's layer (the proper import statement made the warning go away, although the code worked fine with the warning). – MusiGenesis Sep 28 '11 at 16:58 Regarding the white rectangle, I'm not sure mainViewController.

ScrollView is the correct view for this. A UIScrollView (which is what I'm guessing your scrollView object is) can contain other views (which are then made scrollable within the bounds of the scroll view) - you probably just need to identify which actual view in your object structure contains the image you want to capture. – MusiGenesis Sep 28 '11 at 17:04.

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.

1 I think the OP wants to copy what's visible in the CGRect into a UIImage - like taking a screen capture. – MusiGenesis Sep 28 '11 at 15:02 no, I want take the visible rect of my scrollview and save it in an UIImageView or UIImage – blackguardian Sep 28 '11 at 15:02.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions