Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to copy image of a UIView to another UIView?

Eric20000Eric20000 Posts: 35Registered Users
edited August 2010 in iOS SDK Development
Hi,

Image and texts are drawn to MainView as usual, I would like to copy the content of MainView to another BackUpView for animation purpose. I guess copy image is much faster than re-draw all the image and text again to BackUpView. Please advise
Post edited by Eric20000 on

Replies

  • scottiphonescottiphone Posts: 802Registered Users
    edited August 2008
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    This will provide a UIImage of the view. There's tradeoffs to doing the same drawing (memory size vs speed) in the drawRect of the other view.
  • mikebmikeb Posts: 43Registered Users
    edited August 2008
    Nice one. Thats very useful for scaling and cropping images.

    I'm loading in an image into an UIImageView "[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];". The UIImageView takes care of scaling and cropping the image. Then I get viewImage from that UIImageView.
    <a href="http://www.zipzest.com"; target="_blank">Colorblind Helper for iPhone</a> - helps people with colorblindness identify and find colors.
  • Eric20000Eric20000 Posts: 35Registered Users
    edited August 2008
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    This will provide a UIImage of the view. There's tradeoffs to doing the same drawing (memory size vs speed) in the drawRect of the other view.

    Thanks for your help, it works but a warning appear on this line

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    warning: no '-renderInContext:' method found
    warning: (Messages without a matching method signature will be assumed
    to return 'id' and accept '...' as arguments.
  • mikebmikeb Posts: 43Registered Users
    edited August 2008
    Eric20000 wrote: »
    Thanks for your help, it works but a warning appear on this line

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    warning: no '-renderInContext:' method found
    warning: (Messages without a matching method signature will be assumed
    to return 'id' and accept '...' as arguments.

    I was getting the same. Since "layer" is part of the UIView parent class and is of type "CALayer" the compiler doesn't know about "renderInContext" (from CALayer).

    To fix use:
    #import <QuartzCore/QuartzCore.h>
    <a href="http://www.zipzest.com"; target="_blank">Colorblind Helper for iPhone</a> - helps people with colorblindness identify and find colors.
  • EricCartmanEricCartman Posts: 23Registered Users
    edited September 2008
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    This will provide a UIImage of the view. There's tradeoffs to doing the same drawing (memory size vs speed) in the drawRect of the other view.

    Hi,

    This works. However, what if I don't want to copy everything in the view, but just specific layer? I have tried creating a _strokeContext variable and drew on it the layer content, and then used the following when copying:
    [self.layer renderInContext:_strokeContext()];
    instead of UIGRAPHICSGetCurrentContext()
    Unfortunately, this time the copied image seems to be blank....

    Any ideas?

    Thanks a lot!
  • metaTheorymetaTheory Posts: 3New Users
    edited September 2008
    Now for a difficult question.
    what if I don't want to copy everything in a view (UIView), just a subrect.
    (This one is driving me crazy.)

    Thanks,
    jt
  • bhearnbhearn Posts: 125Registered Users
    edited September 2008
  • metaTheorymetaTheory Posts: 3New Users
    edited September 2008
    >OK, you want to use CGImageCreateWithImageInRect( CGImageRef, CGRect).

    Yes, I want too.
    BUT How do I get a CGImageRef from a UIView?

    -jt
  • bhearnbhearn Posts: 125Registered Users
    edited September 2008
    metaTheory wrote: »
    >OK, you want to use CGImageCreateWithImageInRect( CGImageRef, CGRect).

    Yes, I want too.
    BUT How do I get a CGImageRef from a UIView?

    -jt

    YOu get it from a UIImage:

    theImage.CGImage
  • metaTheorymetaTheory Posts: 3New Users
    edited September 2008
    >YOu get it from a UIImage:

    The problem here is that a UIView does NOT have a UIImage.
    A UIImageView has a UIImage. theImage.CGImage

    So I repeat:
    How do I get a CGImageRef from a UIView? Not a UIImageView.

    -jt
  • bhearnbhearn Posts: 125Registered Users
    edited September 2008
    Go back to the second post in this thread.
  • flipconversionflipconversion Posts: 83Registered Users
    edited January 2009
    This method is great, but it doesn't seem to capture my OpenGL layers - any advice?
  • hxbahxba Posts: 5New Users
    edited May 2010
    This method is great, but it doesn't seem to capture my OpenGL layers - any advice?

    hi sir, i face the problem too, do you find any solve yet ?
  • mokargasmokargas Posts: 68Registered Users
    edited August 2010
    This does not work for video for some reason, for instance, using MPMovieController.

    Any ideas? The hardware capture method works fine, but I haven't found a way to do this with the SDK yet.
Sign In or Register to comment.