how to make a fadein animation effect on imageView
i m pulling an image from an url and showing it in an imageview.what i want is an fadein animation effect when the image gets loaded from the url.i m using a background threading below is the code..
NSOperationQueue *theQueue=[[[NSOperationQueue alloc]init]autorelease];
NSInvocationOperation *theOp=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadImage) object:nil];
[theQueue addOperation:theOp];
}
-(void)loadImage
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
[activityIndicator startAnimating];
NSString *imagefile = [image objectAtIndex:0];
NSURL *url1=[NSURL URLWithString:imagefile];
NSData *data = [NSData dataWithContentsOfURL:url1];
UIImage *ui=[[UIImage alloc]initWithData:data];
NSLog(@"awesome:%开发者_JAVA技巧@",ui);
[ui beginAnimations:@"" context:nil];
bigbanner.image=ui;
[activityIndicator stopAnimating];
[pool release];
}
Yes what you say is absolutely correct and thats the way to do it. I wrote this method & always use this to Fade in my image. I deal with CALayer for this. You need to import Quartz framework for this.
#import <QuartzCore/QuartzCore.h>
...
- (void)fadeInLayer:(CALayer *)l
{
CABasicAnimation *fadeInAnimate = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimate.duration = 0.5;
fadeInAnimate.repeatCount = 1;
fadeInAnimate.autoreverses = NO;
fadeInAnimate.fromValue = [NSNumber numberWithFloat:0.0];
fadeInAnimate.toValue = [NSNumber numberWithFloat:1.0];
fadeInAnimate.removedOnCompletion = YES;
[l addAnimation:fadeInAnimate forKey:@"animateOpacity"];
return;
}
Usage: If you are using this from your current file, then -
[self fadeInLayer:imageView.layer];
Hope this helps
Editing my answer as I need some screenshot:
open your Interface builder , do want I did even with an empty image (it will be the place holder for the image you are about to update from the web later)
later just do this
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.75];
[UIView setAnimationDelegate:self];
theImage.alpha = 0.0;
[UIView commitAnimations];
}
Simple as that.
No need for autorelease pools if you use NSBlockOperation.
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com/intl/en_com/images/srpr/logo3w.png"]];
UIImage *image = [[UIImage alloc]initWithData:data];
self.bigbanner = [[UIImageView alloc] initWithImage:image];
self.bigbanner.alpha=0.;
}];
[operation setCompletionBlock:^{
[UIView animateWithDuration:.5
animations:^{ bigbanner.alpha = 1.0; }
completion:^(BOOL finished){ }];
}];
[queue addOperation:operation];
Maybe it should dispatch_async on the main thread to update the image, but works anyway. This won't do anything unless you have previously connected the UIImageView from your nib to an ivar on your class (ShiShi's answer explains how).
精彩评论