这里有两张设置的方法
第一种:使用第三方框架sdwebimage
#import"UIImage+GIF.h"
NSString*name =@"750-611"; //图片的名字
NSString*filePath = [[NSBundlebundleWithPath:[[NSBundlemainBundle]bundlePath]]pathForResource:nameofType:@"gif"];
NSData*imageData = [NSDatadataWithContentsOfFile:filePath];
_dImg.backgroundColor= [UIColorclearColor];
_dImg.image= [UIImagesd_animatedGIFWithData:imageData];
第二种:引入UIImageView的子类ZLGIFImageView(自定义的类)
#import"ZLGIFImageView.h"
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"750-611" ofType:@"gif"];
NSData* imageData = [NSData dataWithContentsOfFile:filePath];
[_dImg setData:imageData];
下边是ZLGIFImageView.h 和 ZLGIFImageView.m文件
ZLGIFImageView.h
#import@interface ZLGIFImageFrame : NSObject {
}
@property (nonatomic) double duration;
@property (nonatomic, retain) UIImage* image;
@end
@interface ZLGIFImageView : UIImageView {
NSInteger _currentImageIndex;
}
@property (nonatomic, retain) NSArray* imageFrameArray;
@property (nonatomic, retain) NSTimer* timer;
//Setting this value to pause or continue animation;
@property (nonatomic) BOOL animating;
- (void)setData:(NSData*)imageData;
- (void)closeAnim;
- (void)startAnim;
- (void)stopAnim;
@end
ZLGIFImageView.m
#import "ZLGIFImageView.h"
#import
@implementation ZLGIFImageFrame
@synthesize image = _image;
@synthesize duration = _duration;
- (void)dealloc
{
[_image release];
[super dealloc];
}
@end
@interface ZLGIFImageView ()
- (void)resetTimer;
- (void)showNextImage;
@end
@implementation ZLGIFImageView
@synthesize imageFrameArray = _imageFrameArray;
@synthesize timer = _timer;
@synthesize animating = _animating;
- (void)closeAnim
{
[self release];
}
- (void)stopAnim
{
[self resetTimer];
[_timer release];
}
- (void)startAnim
{
[self resetTimer];
_currentImageIndex = -1;
_animating = YES;
[self showNextImage];
}
- (void)dealloc
{
[self resetTimer];
[_imageFrameArray release];
[_timer release];
[super dealloc];
}
- (void)resetTimer {
if (_timer && _timer.isValid) {
[_timer invalidate];
}
self.timer = nil;
}
- (void)setData:(NSData *)imageData {
if (!imageData) {
return;
}
[self resetTimer];
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
size_t count = CGImageSourceGetCount(source);
NSMutableArray* tmpArray = [NSMutableArray array];
for (size_t i = 0; i < count; i++) {
ZLGIFImageFrame* gifImage = [[[ZLGIFImageFrame alloc] init] autorelease];
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
gifImage.image = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
NSDictionary* frameProperties = [(NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, i, NULL) autorelease];
gifImage.duration = [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
gifImage.duration = MAX(gifImage.duration, 0.01);
[tmpArray addObject:gifImage];
CGImageRelease(image);
}
CFRelease(source);
self.imageFrameArray = nil;
if (tmpArray.count > 1) {
self.imageFrameArray = tmpArray;
_currentImageIndex = -1;
_animating = YES;
[self showNextImage];
} else {
self.image = [UIImage imageWithData:imageData];
}
}
- (void)setImage:(UIImage *)image {
[super setImage:image];
[self resetTimer];
self.imageFrameArray = nil;
_animating = NO;
}
- (void)showNextImage {
if (!_animating) {
return;
}
_currentImageIndex = (++_currentImageIndex) % _imageFrameArray.count;
ZLGIFImageFrame* gifImage = [_imageFrameArray objectAtIndex:_currentImageIndex];
[super setImage:[gifImage image]];
self.timer = [NSTimer scheduledTimerWithTimeInterval:gifImage.duration target:self selector:@selector(showNextImage) userInfo:nil repeats:NO];
}
- (void)setAnimating:(BOOL)animating {
if (_imageFrameArray.count < 2) {
_animating = animating;
return;
}
if (!_animating && animating) {
_animating = animating;
if (!_timer) {
[self showNextImage];
}
} else if (_animating && !animating) {
_animating = animating;
[self resetTimer];
}
}
@end