11
11
12
12
@implementation UIImage (GIF)
13
13
14
- +(UIImage*)animatedGIFWithData : (NSData *)data {
15
- if (!data) {
14
+ + (UIImage *)animatedGIFWithData : (NSData *)data
15
+ {
16
+ if (!data)
17
+ {
16
18
return nil ;
17
19
}
18
20
19
21
CGImageSourceRef source = CGImageSourceCreateWithData ((__bridge CFDataRef)data, NULL );
20
22
21
23
size_t count = CGImageSourceGetCount (source);
22
- NSMutableArray * images = [NSMutableArray array ];
24
+ NSMutableArray * images = [NSMutableArray array ];
23
25
24
26
NSTimeInterval duration = 0 .0f ;
25
27
26
- for (size_t i = 0 ; i < count; i++) {
28
+ for (size_t i = 0 ; i < count; i++)
29
+ {
27
30
CGImageRef image = CGImageSourceCreateImageAtIndex (source, i, NULL );
28
31
29
- NSDictionary * frameProperties = CFBridgingRelease (CGImageSourceCopyPropertiesAtIndex (source, i, NULL ));
32
+ NSDictionary * frameProperties = CFBridgingRelease (CGImageSourceCopyPropertiesAtIndex (source, i, NULL ));
30
33
duration += [[[frameProperties objectForKey: (NSString *)kCGImagePropertyGIFDictionary ] objectForKey: (NSString *)kCGImagePropertyGIFDelayTime ] doubleValue ];
31
34
32
35
[images addObject: [UIImage imageWithCGImage: image scale: [UIScreen mainScreen ].scale orientation: UIImageOrientationUp]];
@@ -36,50 +39,59 @@ +(UIImage*)animatedGIFWithData:(NSData *)data {
36
39
37
40
CFRelease (source);
38
41
39
- if (!duration) {
42
+ if (!duration)
43
+ {
40
44
duration = (1 .0f /10 .0f )*count;
41
45
}
42
46
43
47
return [UIImage animatedImageWithImages: images duration: duration];
44
48
}
45
49
46
- +(UIImage*)animatedGIFNamed : (NSString *)name {
50
+ + (UIImage *)animatedGIFNamed : (NSString *)name
51
+ {
47
52
CGFloat scale = [UIScreen mainScreen ].scale ;
48
53
49
- if (scale > 1 .0f ) {
50
- NSString * retinaPath = [[NSBundle mainBundle ] pathForResource: [name stringByAppendingString: @" @2x" ] ofType: @" gif" ];
54
+ if (scale > 1 .0f )
55
+ {
56
+ NSString *retinaPath = [[NSBundle mainBundle ] pathForResource: [name stringByAppendingString: @" @2x" ] ofType: @" gif" ];
51
57
52
- NSData * data = [NSData dataWithContentsOfFile: retinaPath];
58
+ NSData * data = [NSData dataWithContentsOfFile: retinaPath];
53
59
54
- if (data) {
60
+ if (data)
61
+ {
55
62
return [UIImage animatedGIFWithData: data];
56
63
}
57
64
58
- NSString * path = [[NSBundle mainBundle ] pathForResource: name ofType: @" gif" ];
65
+ NSString * path = [[NSBundle mainBundle ] pathForResource: name ofType: @" gif" ];
59
66
60
67
data = [NSData dataWithContentsOfFile: path];
61
68
62
- if (data) {
69
+ if (data)
70
+ {
63
71
return [UIImage animatedGIFWithData: data];
64
72
}
65
73
66
74
return [UIImage imageNamed: name];
67
75
}
68
- else {
69
- NSString * path = [[NSBundle mainBundle ] pathForResource: name ofType: @" gif" ];
76
+ else
77
+ {
78
+ NSString *path = [[NSBundle mainBundle ] pathForResource: name ofType: @" gif" ];
70
79
71
- NSData * data = [NSData dataWithContentsOfFile: path];
80
+ NSData * data = [NSData dataWithContentsOfFile: path];
72
81
73
- if (data) {
82
+ if (data)
83
+ {
74
84
return [UIImage animatedGIFWithData: data];
75
85
}
76
86
77
87
return [UIImage imageNamed: name];
78
88
}
79
89
}
80
90
81
- -(UIImage*)animatedImageByScalingAndCroppingToSize : (CGSize)size {
82
- if (CGSizeEqualToSize (self.size , size) || CGSizeEqualToSize (size, CGSizeZero)) {
91
+ - (UIImage *)animatedImageByScalingAndCroppingToSize : (CGSize)size
92
+ {
93
+ if (CGSizeEqualToSize (self.size , size) || CGSizeEqualToSize (size, CGSizeZero))
94
+ {
83
95
return self;
84
96
}
85
97
@@ -91,17 +103,22 @@ -(UIImage*)animatedImageByScalingAndCroppingToSize:(CGSize)size {
91
103
CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor :heightFactor;
92
104
scaledSize.width = self.size .width * scaleFactor;
93
105
scaledSize.height = self.size .height * scaleFactor;
94
- if (widthFactor > heightFactor) {
106
+
107
+ if (widthFactor > heightFactor)
108
+ {
95
109
thumbnailPoint.y = (size.height - scaledSize.height ) * 0.5 ;
96
- } else if (widthFactor < heightFactor) {
110
+ }
111
+ else if (widthFactor < heightFactor)
112
+ {
97
113
thumbnailPoint.x = (size.width - scaledSize.width ) * 0.5 ;
98
114
}
99
115
100
- NSMutableArray * scaledImages = [NSMutableArray array ];
116
+ NSMutableArray * scaledImages = [NSMutableArray array ];
101
117
102
118
UIGraphicsBeginImageContextWithOptions (size, NO , 0.0 );
103
119
104
- for (UIImage* image in self.images ) {
120
+ for (UIImage *image in self.images )
121
+ {
105
122
[image drawInRect: CGRectMake (thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
106
123
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext ();
107
124
0 commit comments