UIImage+MultiFormat.m 3.1 KB

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