Gif图片的解析

来源:互联网 发布:c语言好考么 编辑:程序博客网 时间:2024/05/16 01:32

  1. //加载gif   
  2.  
  3. 02     
  4.  
  5. 03   NSString *filePath = [[NSBundle mainBundle]pathForResource:@"bai3" ofType:@"gif"];   
  6.  
  7. 04     
  8.  
  9. 05     NSData *data = [NSData dataWithContentsOfFile:filePath];   
  10.  
  11. 06     
  12.  
  13. 07     CGImageSourceRef gif = CGImageSourceCreateWithData((CFDataRef)data, nil);   
  14.  
  15. 08     
  16.  
  17. 09  //获取gif的各种属性   
  18.  
  19. 10     
  20.  
  21. 11     CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));   
  22.  
  23. 12     
  24.  
  25. 13     NSLog(@"_______%@",gifprops);   
  26.  
  27. 14     
  28.  
  29. 15     
  30.  
  31. 16     NSInteger count =CGImageSourceGetCount(gif);   
  32.  
  33. 17     
  34.  
  35. 18     NSLog(@"________%d",count);   
  36.  
  37. 19     
  38.  
  39. 20     
  40.  
  41. 21    CFDictionaryRef gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);   
  42.  
  43. 22     
  44.  
  45. 23  CFDictionaryRef delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime);   
  46.  
  47. 24     
  48.  
  49. 25     NSLog(@"_______%@",delay);    
  50.  
  51. 26     
  52.  
  53. 27     
  54.  
  55. 28  //[gifDic objectForKey:(NSString *)kCGImagePropertyGIFDelayTime];   
  56.  
  57. 29     
  58.  
  59. 30     //    NSNumber * w = CFDictionaryGetValue(gifprops, @"PixelWidth");   
  60.  
  61. 31     
  62.  
  63. 32     //    NSNumber * h =CFDictionaryGetValue(gifprops, @"PixelHeight");   
  64.  
  65. 33     
  66.  
  67. 34     //    float totalDuration = delay.doubleValue * count;   
  68.  
  69. 35     
  70.  
  71. 36     //    float pixelWidth = w.intValue;   
  72.  
  73. 37     
  74.  
  75. 38     //    float pixelHeight = h.intValue;   
  76.  
  77. 39     
  78.  
  79. 40   //将gif解析成UIImage类型对象,并加进images数组中     
  80.  
  81. 41     
  82.  
  83. 42     
  84.  
  85. 43     NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];   
  86.  
  87. 44     
  88.  
  89. 45     for(int index=0;index<count;index++)   
  90.  
  91. 46     
  92.  
  93. 47     {   
  94.  
  95. 48     
  96.  
  97. 49         CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil);   
  98.  
  99. 50     
  100.  
  101. 51         UIImage *img = [UIImage imageWithCGImage:ref];   
  102.  
  103. 52     
  104.  
  105. 53         [images addObject:img];   
  106.  
  107. 54     
  108.  
  109. 55         CFRelease(ref);   
  110.  
  111. 56     
  112.  
  113. 57     }   
  114.  
  115. 58     
  116.  
  117. 59     CFRelease(gifprops);   
  118.  
  119. 60     
  120.  
  121. 61     CFRelease(gif);  


0 0