0

In my app:

  • I have a view (UIView *myView) wihth clipsToBound = YES;

  • I have a button to do changing the origin of bounds property:

CGRect newRect = myView.bounds;
newRect.origin.x += 100;
myView.bounds = newRect;
myView.layer.frame = newRect;
  • Then I get the image from the view:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage_after = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage_after, nil, nil, nil);

It produces the image I don't expected. I want a image as I see in the iPhone's screen.

Link for the code here: http://www.mediafire.com/?ufr1q8lbd434wu1

Please help me!

2
  • I don't think the myView.layer.frame = newRect; is necessary, try removing it.
    – jbat100
    Oct 21, 2011 at 13:43
  • Dear, Moving it don't resolve the problem.
    – vietstone
    Oct 24, 2011 at 0:31

1 Answer 1

1

You don't want to render the image itself in your context - that will always render in the same way (you're not altering the image, you're jsut moving how far up the view it is).

You want to render the image's parent view like this :

UIGraphicsBeginImageContext(myView.superview.bounds.size);
[myView.superview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage_after = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage_after, nil, nil, nil);

EDIT

However, you might not want to render everything in your superview :)

Your views might look something like

MainView
   ImageView (myView)
   UIButton (ok button)
   UIButton (cancel button)

Here, rendering your image's superview will render MainView - including the buttons!

You need to add another view into your hierachy like this :

MainView
  UIView (enpty uiview)
    ImageView (myView)
  UIButton (ok button)
  UIButton (cancel button)

Now, when you render your image's superview, it's only got the image inside it - the buttons don't get rendered :)

3
  • Dear deanWombourne. Your solution works great, it renders the image as the view display in the screen. But I want to render only the myView, not the superView. Is there any way?
    – vietstone
    Oct 24, 2011 at 0:47
  • Link for my original unexpected code here: mediafire.com/?ufr1q8lbd434wu1
    – vietstone
    Oct 24, 2011 at 3:42
  • You need to add another view into your hierarchy - take a look at my edit for more details. Oct 24, 2011 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.