当前位置:编程学习 > wap >>

IOS GIF播放, 包含UITableViewCell中正常播放

 
本程序依赖第三方库UIImage+animatedImageWithGIF, 但是有小幅度修改
 1 //
 2 //  UIImage+animatedImageWithGIF.h
 3 //
 4 //  Created by YuAo on 2/24/12.
 5 //  Copyright (c) 2012 eico design. All rights reserved.
 6 //
 7 //  Note: 
 8 //        ImageIO.framework is needed.
 9 //        This lib is only available on iOS 4+
10 
11 #import <Foundation/Foundation.h>
12 
13 @interface UIImageView(animatedImageViewWithGIF)
14 + (UIImageView *)imageViewWithGIFData:(NSData *)data;
15 //+ (UIImageView *)imageViewWithGIFURL:(NSURL *)url;
16 @end
 
 
 
 1 //
 2 //  UIImage+animatedImageWithGIF.m
 3 //
 4 //  Created by YuAo on 2/24/12.
 5 //  Copyright (c) 2012 eico design. All rights reserved.
 6 //
 7 
 8 #import "UIImageView+imageViewWithGIF.h"
 9 #import <ImageIO/ImageIO.h>
10 
11 #if __has_feature(objc_arc)
12     #define toCF (__bridge CFTypeRef)
13     #define ARCCompatibleAutorelease(object) object
14 #else
15     #define toCF (CFTypeRef)
16     #define ARCCompatibleAutorelease(object) [object autorelease]
17 #endif
18 
19 @implementation UIImageView(animatedImageViewWithGIF)
20 
21 + (UIImageView *)imageViewWithAnimatedGIFImageSource:(CGImageSourceRef) source 
22                                          andDuration:(NSTimeInterval) duration {
23     if (!source) return nil;
24     size_t count = CGImageSourceGetCount(source);
25     NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];
26 
27     for (size_t i = 0; i < count; ++i) {
28         CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, i, NULL);
29         if (!cgImage)
30             return nil;
31         [images addObject:[UIImage imageWithCGImage:cgImage]];
32         CGImageRelease(cgImage);
33     }
34     UIImageView *imageView = [[UIImageView alloc] init];
35     if ([images count] > 0) {
36         imageView.image = [images objectAtIndex:0];
37     }
38     [imageView setAnimationImages:images];
39     [imageView setHighlighted:NO];
40 //    [imageView setHighlightedAnimationImages:images];
41     [imageView setAnimationDuration:duration];
42     [imageView sizeToFit];
43 //    [imageView startAnimating];
44     [imageView performSelector:@selector(startAnimating) withObject:nil afterDelay:0.3f];//tableviewcell中需要延时播放动画, 否则会停住不动
45 //    NSLog(@"begin");
46     return ARCCompatibleAutorelease(imageView);
47 }
48 
49 + (NSTimeInterval)durationForGifData:(NSData *)data {
50     char graphicControlExtensionStartBytes[] = {0x21,0xF9,0x04};
51     double duration=0;
52     NSRange dataSearchLeftRange = NSMakeRange(0, data.length);
53     while(YES){
54         NSRange frameDescriptorRange = [data rangeOfData:[NSData dataWithBytes:graphicControlExtensionStartBytes 
55                                                                         length:3] 
56                                                  options:NSDataSearchBackwards
57                                                    range:dataSearchLeftRange];
58         if(frameDescriptorRange.location != NSNotFound){
59             NSData *durationData = [data subdataWithRange:NSMakeRange(frameDescriptorRange.location+4, 2)];
60             unsigned char buffer[2];
61             [durationData getBytes:buffer];
62             double delay = (buffer[0] | buffer[1] << 8);
63             duration += delay;
64             dataSearchLeftRange = NSMakeRange(0, frameDescriptorRange.location);
65         }else{
66             break;
67         }
68     }
69     return duration/100;
70 }
71 
72 + (UIImageView *)imageViewWithGIFData:(NSData *)data{
73     NSTimeInterval duration = [self durationForGifData:data];
74     CGImageSourceRef source = CGImageSourceCreateWithData(toCF data, NULL);
75     UIImageView *imageView = [UIImageView imageViewWithAnimatedGIFImageSource:source andDuration:duration]; 
76     CFRelease(source);
77     return imageView;
78 }
79 
80 //+ (UIImageView *)imageViewWithGIFURL:(NSURL *)url{
81 //    NSData *data = [NSData dataWithContentsOfURL:url];
82 //    return [UIImageView imageViewWithGIFData:data];
83 //}
84 
85 @end
86 
 
 1 //
 2 //  testViewController.h
 3 //  UIImageViewGIF
 4 //
 5 //  Created by khan.lau on 13-2-26.
 6 //
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface testViewController : UITableViewController
12 
13 @end
 
 
  1 //  在UITableview中显示的范例
  2 //  testViewController.m
  3 //  UIImageViewGIF
  4 //
  5 //  Created by khan.lau on 13-2-26.
  6 //
  7 //
  8 
  9 #import "testViewController.h"
 10 #import "UIImageView+imageViewWithGIF.h"
 11 
 12 @interface testViewController ()
 13 
 14 @end
 15 
 16 @implementation testViewController补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,