Skip to content

Commit 6d6796c

Browse files
committed
explicit webp detection
1 parent 77fad5c commit 6d6796c

File tree

7 files changed

+75
-73
lines changed

7 files changed

+75
-73
lines changed

NSData+ImageContentType.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//
2+
// Created by Fabrice Aneche on 06/01/14.
3+
// Copyright (c) 2014 Dailymotion. All rights reserved.
4+
//
5+
6+
#import <Foundation/Foundation.h>
7+
8+
@interface NSData (ImageContentType)
9+
+ (NSString *)contentTypeForImageData:(NSData *)data;
10+
@end

NSData+ImageContentType.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created by Fabrice Aneche on 06/01/14.
3+
// Copyright (c) 2014 Dailymotion. All rights reserved.
4+
//
5+
6+
#import "NSData+ImageContentType.h"
7+
8+
9+
@implementation NSData (ImageContentType)
10+
11+
+ (NSString *)contentTypeForImageData:(NSData *)data
12+
{
13+
uint8_t c;
14+
[data getBytes:&c length:1];
15+
switch (c) {
16+
case 0xFF:
17+
return @"image/jpeg";
18+
case 0x89:
19+
return @"image/png";
20+
case 0x47:
21+
return @"image/gif";
22+
case 0x49:
23+
case 0x4D:
24+
return @"image/tiff";
25+
case 0x52:
26+
// R as RIFF for WEBP
27+
if ([data length] < 12)
28+
return nil;
29+
30+
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
31+
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"])
32+
return @"image/webp";
33+
34+
return nil;
35+
}
36+
return nil;
37+
}
38+
39+
@end

SDWebImage.xcodeproj/project.pbxproj

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
/* End PBXAggregateTarget section */
2323

2424
/* Begin PBXBuildFile section */
25+
1797E364CA5D259B8CEDF8EE /* NSData+ImageContentType.h in Headers */ = {isa = PBXBuildFile; fileRef = 1797E851007E466318B38B47 /* NSData+ImageContentType.h */; };
26+
1797E37E262405DB1BFEDFFC /* NSData+ImageContentType.m in Sources */ = {isa = PBXBuildFile; fileRef = 1797E3D4E27D504ACD827BE1 /* NSData+ImageContentType.m */; };
27+
1797E59FBF11E6588BC89B66 /* NSData+ImageContentType.h in Headers */ = {isa = PBXBuildFile; fileRef = 1797E851007E466318B38B47 /* NSData+ImageContentType.h */; };
28+
1797E75161F3C9E8494E43E6 /* NSData+ImageContentType.h in Headers */ = {isa = PBXBuildFile; fileRef = 1797E851007E466318B38B47 /* NSData+ImageContentType.h */; };
29+
1797EBFB62F22E5C0A26A75D /* NSData+ImageContentType.m in Sources */ = {isa = PBXBuildFile; fileRef = 1797E3D4E27D504ACD827BE1 /* NSData+ImageContentType.m */; };
30+
1797ED4CD80EA8BD405365C3 /* NSData+ImageContentType.m in Sources */ = {isa = PBXBuildFile; fileRef = 1797E3D4E27D504ACD827BE1 /* NSData+ImageContentType.m */; };
2531
530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 530E49E71646388E002868E7 /* SDWebImageOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
2632
530E49E916464C26002868E7 /* SDWebImageOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 530E49E71646388E002868E7 /* SDWebImageOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
2733
530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 530E49E316460AE2002868E7 /* SDWebImageDownloaderOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -77,7 +83,6 @@
7783
537D957C17ECC1FE0097C263 /* SDWebImageDownloaderOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 530E49E416460AE2002868E7 /* SDWebImageDownloaderOperation.m */; };
7884
537D957D17ECC1FE0097C263 /* SDWebImageCompat.m in Sources */ = {isa = PBXBuildFile; fileRef = 5340674F167780C40042B59E /* SDWebImageCompat.m */; };
7985
537D957E17ECC1FE0097C263 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
80-
537D957F17ECC1FE0097C263 /* NSData+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CCC172DC33A00419892 /* NSData+GIF.m */; };
8186
537D958017ECC1FE0097C263 /* UIImage+MultiFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */; };
8287
537D958117ECC1FE0097C263 /* UIImage+WebP.m in Sources */ = {isa = PBXBuildFile; fileRef = 53EDFB921762547C00698166 /* UIImage+WebP.m */; };
8388
537D958217ECC1FE0097C263 /* webp.c in Sources */ = {isa = PBXBuildFile; fileRef = 53EDFC9117625BE300698166 /* webp.c */; };
@@ -119,7 +124,6 @@
119124
537D95A817ECC1FE0097C263 /* SDWebImageOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 530E49E71646388E002868E7 /* SDWebImageOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
120125
537D95A917ECC1FE0097C263 /* SDWebImageDownloaderOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 530E49E316460AE2002868E7 /* SDWebImageDownloaderOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
121126
537D95AA17ECC1FE0097C263 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; };
122-
537D95AB17ECC1FE0097C263 /* NSData+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CCB172DC33A00419892 /* NSData+GIF.h */; };
123127
537D95AC17ECC1FE0097C263 /* UIImage+MultiFormat.h in Headers */ = {isa = PBXBuildFile; fileRef = 53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */; };
124128
537D95AD17ECC1FE0097C263 /* UIImage+WebP.h in Headers */ = {isa = PBXBuildFile; fileRef = 53EDFB911762547C00698166 /* UIImage+WebP.h */; };
125129
537D95AE17ECC1FE0097C263 /* webpi.h in Headers */ = {isa = PBXBuildFile; fileRef = 53EDFC9017625BE300698166 /* webpi.h */; };
@@ -187,10 +191,6 @@
187191
A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CC5172DC28500419892 /* UIImage+GIF.h */; };
188192
A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
189193
A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CC6172DC28500419892 /* UIImage+GIF.m */; };
190-
A18A6CCD172DC33A00419892 /* NSData+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CCB172DC33A00419892 /* NSData+GIF.h */; };
191-
A18A6CCE172DC33A00419892 /* NSData+GIF.h in Headers */ = {isa = PBXBuildFile; fileRef = A18A6CCB172DC33A00419892 /* NSData+GIF.h */; };
192-
A18A6CCF172DC33A00419892 /* NSData+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CCC172DC33A00419892 /* NSData+GIF.m */; };
193-
A18A6CD0172DC33A00419892 /* NSData+GIF.m in Sources */ = {isa = PBXBuildFile; fileRef = A18A6CCC172DC33A00419892 /* NSData+GIF.m */; };
194194
/* End PBXBuildFile section */
195195

196196
/* Begin PBXContainerItemProxy section */
@@ -204,6 +204,8 @@
204204
/* End PBXContainerItemProxy section */
205205

206206
/* Begin PBXFileReference section */
207+
1797E3D4E27D504ACD827BE1 /* NSData+ImageContentType.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+ImageContentType.m"; sourceTree = SOURCE_ROOT; };
208+
1797E851007E466318B38B47 /* NSData+ImageContentType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+ImageContentType.h"; sourceTree = SOURCE_ROOT; };
207209
530E49E316460AE2002868E7 /* SDWebImageDownloaderOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDWebImageDownloaderOperation.h; sourceTree = "<group>"; };
208210
530E49E416460AE2002868E7 /* SDWebImageDownloaderOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageDownloaderOperation.m; sourceTree = "<group>"; };
209211
530E49E71646388E002868E7 /* SDWebImageOperation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDWebImageOperation.h; sourceTree = "<group>"; };
@@ -277,8 +279,6 @@
277279
53FB894814D35E9E0020B787 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
278280
A18A6CC5172DC28500419892 /* UIImage+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+GIF.h"; sourceTree = "<group>"; };
279281
A18A6CC6172DC28500419892 /* UIImage+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+GIF.m"; sourceTree = "<group>"; };
280-
A18A6CCB172DC33A00419892 /* NSData+GIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+GIF.h"; sourceTree = "<group>"; };
281-
A18A6CCC172DC33A00419892 /* NSData+GIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+GIF.m"; sourceTree = "<group>"; };
282282
/* End PBXFileReference section */
283283

284284
/* Begin PBXFrameworksBuildPhase section */
@@ -365,8 +365,6 @@
365365
children = (
366366
53EDFB8817623F7C00698166 /* UIImage+MultiFormat.h */,
367367
53EDFB8917623F7C00698166 /* UIImage+MultiFormat.m */,
368-
A18A6CCB172DC33A00419892 /* NSData+GIF.h */,
369-
A18A6CCC172DC33A00419892 /* NSData+GIF.m */,
370368
A18A6CC5172DC28500419892 /* UIImage+GIF.h */,
371369
A18A6CC6172DC28500419892 /* UIImage+GIF.m */,
372370
53EDFB911762547C00698166 /* UIImage+WebP.h */,
@@ -377,6 +375,8 @@
377375
53922D94148C56230056699D /* UIButton+WebCache.m */,
378376
53922D95148C56230056699D /* UIImageView+WebCache.h */,
379377
53922D96148C56230056699D /* UIImageView+WebCache.m */,
378+
1797E851007E466318B38B47 /* NSData+ImageContentType.h */,
379+
1797E3D4E27D504ACD827BE1 /* NSData+ImageContentType.m */,
380380
);
381381
name = Categories;
382382
sourceTree = "<group>";
@@ -504,7 +504,6 @@
504504
530E49E916464C26002868E7 /* SDWebImageOperation.h in Headers */,
505505
530E49EB16464C7F002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
506506
A18A6CC8172DC28500419892 /* UIImage+GIF.h in Headers */,
507-
A18A6CCE172DC33A00419892 /* NSData+GIF.h in Headers */,
508507
53EDFB8B17623F7C00698166 /* UIImage+MultiFormat.h in Headers */,
509508
53E481E017C300F6003E8957 /* alpha_processing.h in Headers */,
510509
53EDFB941762547D00698166 /* UIImage+WebP.h in Headers */,
@@ -522,6 +521,7 @@
522521
53EDFCF617625F4100698166 /* utils.h in Headers */,
523522
53EDFCFC17625F5F00698166 /* filters.h in Headers */,
524523
53EDFD0217625F7900698166 /* thread.h in Headers */,
524+
1797E75161F3C9E8494E43E6 /* NSData+ImageContentType.h in Headers */,
525525
);
526526
runOnlyForDeploymentPostprocessing = 0;
527527
};
@@ -540,8 +540,8 @@
540540
530E49E816464C25002868E7 /* SDWebImageOperation.h in Headers */,
541541
530E49EA16464C7C002868E7 /* SDWebImageDownloaderOperation.h in Headers */,
542542
A18A6CC7172DC28500419892 /* UIImage+GIF.h in Headers */,
543-
A18A6CCD172DC33A00419892 /* NSData+GIF.h in Headers */,
544543
53EDFB8A17623F7C00698166 /* UIImage+MultiFormat.h in Headers */,
544+
1797E59FBF11E6588BC89B66 /* NSData+ImageContentType.h in Headers */,
545545
);
546546
runOnlyForDeploymentPostprocessing = 0;
547547
};
@@ -560,7 +560,6 @@
560560
537D95A817ECC1FE0097C263 /* SDWebImageOperation.h in Headers */,
561561
537D95A917ECC1FE0097C263 /* SDWebImageDownloaderOperation.h in Headers */,
562562
537D95AA17ECC1FE0097C263 /* UIImage+GIF.h in Headers */,
563-
537D95AB17ECC1FE0097C263 /* NSData+GIF.h in Headers */,
564563
537D95AC17ECC1FE0097C263 /* UIImage+MultiFormat.h in Headers */,
565564
537D95AD17ECC1FE0097C263 /* UIImage+WebP.h in Headers */,
566565
537D95AE17ECC1FE0097C263 /* webpi.h in Headers */,
@@ -578,6 +577,7 @@
578577
537D95BA17ECC1FE0097C263 /* utils.h in Headers */,
579578
537D95BB17ECC1FE0097C263 /* filters.h in Headers */,
580579
537D95BC17ECC1FE0097C263 /* thread.h in Headers */,
580+
1797E364CA5D259B8CEDF8EE /* NSData+ImageContentType.h in Headers */,
581581
);
582582
runOnlyForDeploymentPostprocessing = 0;
583583
};
@@ -726,7 +726,6 @@
726726
531041CB157EAFA400BBABC3 /* MKAnnotationView+WebCache.m in Sources */,
727727
530E49ED16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */,
728728
A18A6CCA172DC28500419892 /* UIImage+GIF.m in Sources */,
729-
A18A6CD0172DC33A00419892 /* NSData+GIF.m in Sources */,
730729
53EDFB8D17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
731730
53EDFB961762547D00698166 /* UIImage+WebP.m in Sources */,
732731
53EDFC9517625BE300698166 /* webp.c in Sources */,
@@ -754,6 +753,7 @@
754753
53EDFCF817625F4100698166 /* utils.c in Sources */,
755754
53EDFCFE17625F5F00698166 /* filters.c in Sources */,
756755
53EDFD0417625F7900698166 /* thread.c in Sources */,
756+
1797EBFB62F22E5C0A26A75D /* NSData+ImageContentType.m in Sources */,
757757
);
758758
runOnlyForDeploymentPostprocessing = 0;
759759
};
@@ -771,8 +771,8 @@
771771
530E49EC16464C84002868E7 /* SDWebImageDownloaderOperation.m in Sources */,
772772
53406750167780C40042B59E /* SDWebImageCompat.m in Sources */,
773773
A18A6CC9172DC28500419892 /* UIImage+GIF.m in Sources */,
774-
A18A6CCF172DC33A00419892 /* NSData+GIF.m in Sources */,
775774
53EDFB8C17623F7C00698166 /* UIImage+MultiFormat.m in Sources */,
775+
1797E37E262405DB1BFEDFFC /* NSData+ImageContentType.m in Sources */,
776776
);
777777
runOnlyForDeploymentPostprocessing = 0;
778778
};
@@ -790,7 +790,6 @@
790790
537D957C17ECC1FE0097C263 /* SDWebImageDownloaderOperation.m in Sources */,
791791
537D957D17ECC1FE0097C263 /* SDWebImageCompat.m in Sources */,
792792
537D957E17ECC1FE0097C263 /* UIImage+GIF.m in Sources */,
793-
537D957F17ECC1FE0097C263 /* NSData+GIF.m in Sources */,
794793
537D958017ECC1FE0097C263 /* UIImage+MultiFormat.m in Sources */,
795794
537D958117ECC1FE0097C263 /* UIImage+WebP.m in Sources */,
796795
537D958217ECC1FE0097C263 /* webp.c in Sources */,
@@ -818,6 +817,7 @@
818817
537D959817ECC1FE0097C263 /* utils.c in Sources */,
819818
537D959917ECC1FE0097C263 /* filters.c in Sources */,
820819
537D959A17ECC1FE0097C263 /* thread.c in Sources */,
820+
1797ED4CD80EA8BD405365C3 /* NSData+ImageContentType.m in Sources */,
821821
);
822822
runOnlyForDeploymentPostprocessing = 0;
823823
};

SDWebImage/NSData+GIF.h

Lines changed: 0 additions & 15 deletions
This file was deleted.

SDWebImage/NSData+GIF.m

Lines changed: 0 additions & 32 deletions
This file was deleted.

SDWebImage/UIImage+GIF.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
77
//
88

9-
#import "NSData+GIF.h"
109
#import <UIKit/UIKit.h>
1110

1211
@interface UIImage (GIF)

SDWebImage/UIImage+MultiFormat.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#import "UIImage+MultiFormat.h"
1010
#import "UIImage+GIF.h"
11-
11+
#import "NSData+ImageContentType.h"
1212
#ifdef SD_WEBP
1313
#import "UIImage+WebP.h"
1414
#endif
@@ -18,22 +18,23 @@ @implementation UIImage (MultiFormat)
1818
+ (UIImage *)sd_imageWithData:(NSData *)data
1919
{
2020
UIImage *image;
21-
22-
if ([data sd_isGIF])
21+
NSString *imageContentType = [NSData contentTypeForImageData:data];
22+
if ([imageContentType isEqualToString:@"image/gif"])
2323
{
2424
image = [UIImage sd_animatedGIFWithData:data];
2525
}
26-
else
27-
{
28-
image = [[UIImage alloc] initWithData:data];
29-
}
30-
3126
#ifdef SD_WEBP
32-
if (!image) // TODO: detect webp signature
27+
else if ([imageContentType isEqualToString:@"image/webp"])
3328
{
3429
image = [UIImage sd_imageWithWebPData:data];
3530
}
3631
#endif
32+
else
33+
{
34+
image = [[UIImage alloc] initWithData:data];
35+
}
36+
37+
3738

3839
return image;
3940
}

0 commit comments

Comments
 (0)