UIImage+MultiFormat.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // UIImage+MultiFormat.m
  3. // SDWebImage
  4. //
  5. // Created by Olivier Poitrey on 07/06/13.
  6. // Copyright (c) 2013 Dailymotion. All rights reserved.
  7. //
  8. #import "UIImage+MultiFormat.h"
  9. #import "UIImage+GIF.h"
  10. #import "NSData+ImageContentType.h"
  11. #import <ImageIO/ImageIO.h>
  12. #ifdef SD_WEBP
  13. #import "UIImage+WebP.h"
  14. #endif
  15. @implementation UIImage (MultiFormat)
  16. + (UIImage *)sd_imageWithData:(NSData *)data {
  17. if (!data) {
  18. return nil;
  19. }
  20. UIImage *image;
  21. NSString *imageContentType = [NSData sd_contentTypeForImageData:data];
  22. if ([imageContentType isEqualToString:@"image/gif"]) {
  23. image = [UIImage sd_animatedGIFWithData:data];
  24. }
  25. #ifdef SD_WEBP
  26. else if ([imageContentType isEqualToString:@"image/webp"])
  27. {
  28. image = [UIImage sd_imageWithWebPData:data];
  29. }
  30. #endif
  31. else {
  32. image = [[UIImage alloc] initWithData:data];
  33. UIImageOrientation orientation = [self sd_imageOrientationFromImageData:data];
  34. if (orientation != UIImageOrientationUp) {
  35. image = [UIImage imageWithCGImage:image.CGImage
  36. scale:image.scale
  37. orientation:orientation];
  38. }
  39. }
  40. return image;
  41. }
  42. +(UIImageOrientation)sd_imageOrientationFromImageData:(NSData *)imageData {
  43. UIImageOrientation result = UIImageOrientationUp;
  44. CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
  45. if (imageSource) {
  46. CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
  47. if (properties) {
  48. CFTypeRef val;
  49. int exifOrientation;
  50. val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
  51. if (val) {
  52. CFNumberGetValue(val, kCFNumberIntType, &exifOrientation);
  53. result = [self sd_exifOrientationToiOSOrientation:exifOrientation];
  54. } // else - if it's not set it remains at up
  55. CFRelease((CFTypeRef) properties);
  56. } else {
  57. //NSLog(@"NO PROPERTIES, FAIL");
  58. }
  59. CFRelease(imageSource);
  60. }
  61. return result;
  62. }
  63. #pragma mark EXIF orientation tag converter
  64. // Convert an EXIF image orientation to an iOS one.
  65. // reference see here: http://sylvana.net/jpegcrop/exif_orientation.html
  66. + (UIImageOrientation) sd_exifOrientationToiOSOrientation:(int)exifOrientation {
  67. UIImageOrientation orientation = UIImageOrientationUp;
  68. switch (exifOrientation) {
  69. case 1:
  70. orientation = UIImageOrientationUp;
  71. break;
  72. case 3:
  73. orientation = UIImageOrientationDown;
  74. break;
  75. case 8:
  76. orientation = UIImageOrientationLeft;
  77. break;
  78. case 6:
  79. orientation = UIImageOrientationRight;
  80. break;
  81. case 2:
  82. orientation = UIImageOrientationUpMirrored;
  83. break;
  84. case 4:
  85. orientation = UIImageOrientationDownMirrored;
  86. break;
  87. case 5:
  88. orientation = UIImageOrientationLeftMirrored;
  89. break;
  90. case 7:
  91. orientation = UIImageOrientationRightMirrored;
  92. break;
  93. default:
  94. break;
  95. }
  96. return orientation;
  97. }
  98. @end