UIImage+MultiFormat.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. //NSLog(@"NO PROPERTIES, FAIL");
  55. }
  56. CFRelease(imageSource);
  57. }
  58. return result;
  59. }
  60. #pragma mark EXIF orientation tag converter
  61. // Convert an EXIF image orientation to an iOS one.
  62. // reference see here: http://sylvana.net/jpegcrop/exif_orientation.html
  63. + (UIImageOrientation) sd_exifOrientationToiOSOrientation:(int)exifOrientation {
  64. UIImageOrientation orientation = UIImageOrientationUp;
  65. switch (exifOrientation) {
  66. case 1:
  67. orientation = UIImageOrientationUp;
  68. break;
  69. case 3:
  70. orientation = UIImageOrientationDown;
  71. break;
  72. case 8:
  73. orientation = UIImageOrientationLeft;
  74. break;
  75. case 6:
  76. orientation = UIImageOrientationRight;
  77. break;
  78. case 2:
  79. orientation = UIImageOrientationUpMirrored;
  80. break;
  81. case 4:
  82. orientation = UIImageOrientationDownMirrored;
  83. break;
  84. case 5:
  85. orientation = UIImageOrientationLeftMirrored;
  86. break;
  87. case 7:
  88. orientation = UIImageOrientationRightMirrored;
  89. break;
  90. default:
  91. break;
  92. }
  93. return orientation;
  94. }
  95. @end