Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2008, 08:48 PM
Sbrocket's Avatar
Junior Member
 
Join Date: Jun 2008
Posts: 2
Default UIImage from any CALayer/UIView

This is something I employ to speed up animations during autorotation. You can get a UIImage to use as you want (such as in a fullscreen UIImageView, as I do) from any CoreAnimation layer (CALayer). Since every UIView has an associated CALayer, you can basically take a snapshot of the current state of any UIView.

Code:
UIGraphicsBeginImageContext(myView.bounds.size);
[myView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
If you have a CALayer rather than a UIView that you want to render, you would simply send the layer renderInContext: directly and use the layer bounds instead of the view bounds.

Currently there is a bug (rdar://6018652) where -[CALayer renderInContext:] is not recognized by the compiler so you will get a warning when using this. The method does definitely exist, though, and its in the headers.

EDIT: Its not actually a bug. Just make sure you import and link in QuartzCore.framework where you use this.

Last edited by Sbrocket; 07-01-2008 at 10:32 PM.
Reply With Quote
  #2 (permalink)  
Old 06-30-2008, 07:50 PM
go2 go2 is offline
Junior Member
 
Join Date: Jun 2008
Posts: 3
Default ...now save it to photo library

Add this line to the end and you can save the image to your photo library -

UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);

or if you need a callback:

UIImageWriteToSavedPhotosAlbum(viewImage, self, (SEL)@selector(viewImage:didFinishSavingWithError: contextInfo:), nil);

- (void)image:(UIImage *)image
didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInfo;
Reply With Quote
  #3 (permalink)  
Old 06-30-2008, 07:55 PM
errrick's Avatar
Junior Member
 
Join Date: Jun 2008
Posts: 12
Default

WOW, this is sick, exactly what I needed!

I had a rotating UITable with lots of rows at 20px height each.. and the rotate effect was painfully slow.. I wonder how Safari managed to do such smooth rotation even on the most overloaded websites, I guess this is the method it uses ?

big thanks, will try it when I get home.
Reply With Quote
  #4 (permalink)  
Old 06-30-2008, 10:26 PM
Sbrocket's Avatar
Junior Member
 
Join Date: Jun 2008
Posts: 2
Default

Quote:
Originally Posted by errrick View Post
WOW, this is sick, exactly what I needed!

I had a rotating UITable with lots of rows at 20px height each.. and the rotate effect was painfully slow.. I wonder how Safari managed to do such smooth rotation even on the most overloaded websites, I guess this is the method it uses ?

big thanks, will try it when I get home.
Its certainly possible. I know the Calculator on the iPhone is actually just one big UIImageView with overlays for when you press the buttons, which is why it animates so smoothly.
Reply With Quote
  #5 (permalink)  
Old 07-17-2008, 07:22 AM
Junior Member
 
Join Date: Jun 2008
Posts: 20
Default cache?

well, if you do cache.. it generates a static image and then rotates that... if not, it renders for each frame... right? so, was it slow without using cache or with? If that description above is accurate -- all rotations should be the same speed.
Reply With Quote
  #6 (permalink)  
Old 11-19-2008, 12:37 AM
Junior Member
 
Join Date: Nov 2008
Location: Bordeaux, France
Posts: 1
Default

Hi,
When I found this code, I thought "cool that's all I need" but visibly not.
I have a UIImageView which is an animation:

Code:
UIImageView *myImage=[[UIImageView alloc] InitWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
myImage.imageView.animationImages= [NSArray arrayWithObjects:[UIImage imageNamed:@"foo1.png"],
                                    [UIImage imageNamed:@"foo2.png"],nil];
		imageView.animationDuration=0.7;
		imageView.animationRepeatCount=0;
[myImage startAnimating];
Now, when the user touch the Iphone screen, I want to know if the pixel he touched was visible or not (my PNG images are RGBA files).
So I tryed with the previous sample...:
Code:
UIGraphicsBeginImageContext(myImage.bounds.size);
[myImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
but when I tried to display snapshot, I have nothing.
So I've tested this code on another UIImageView like this:
Code:
UIImageView *myImage=[[UIImageView alloc] InitWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
myImage.imageView.animationImages= [NSArray arrayWithObjects:[UIImage imageNamed:@"foo1.png"],
                                    [UIImage imageNamed:@"foo2.png"],nil];
myImage.image=[UIImage imageNamed:@"foo3.png"]
		imageView.animationDuration=0.7;
		imageView.animationRepeatCount=0;
[myImage startAnimating];
And this give me a snapshot of foo3.png which is not what I want.
So have you got any idea how can I get the current image played?
I tried also with a timer which would indicate me time elapsed since the beginning of the animation but it's a bad solution and it doesn't work too.
Thanks for your replies.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:58 AM.