第二篇
前言
本篇是和GIF相關的一個UIImage的分類。主要提供了三個方法:
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name
----- 根據名稱獲取圖片+ (UIImage *)sd_animatedGIFWithData:(NSData *)data
----- 根據NSData獲取圖片- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size
----- 修改圖片到指定的尺寸
UIImage的size,scale屬性
我們先不管圖片的更高級的知識,我們簡單的對size和scale這兩個屬性做一下介紹。
注意:如果要獲取一個圖片的尺寸,不是直接使用image.size
,而是使用image.size
*image.scale
。當然,這是偽代碼。原因就是我們在獲取size的時候。使用的是Point坐標,而圖片的尺寸是以像素為參照的。系統為我們處理了這兩種坐標系的轉換工作。
我們用一個例子來演示上邊的內容:
UIImage *image = [UIImage imageNamed:@"photo_delete"];
NSLog(@"-----尺寸:(%f %f)", image.size.width, image.size.height);
打印結果為:
-----尺寸:(18.000000 18.000000)
可以看出來。使用size這個屬性是不對的。該圖片的實際尺寸為:
那我們修改下代碼:
UIImage *image = [UIImage imageNamed:@"photo_delete"];
NSLog(@"-----尺寸:(%f %f)", image.size.width * image.scale, image.size.height * image.scale);
打印結果如下:
-----尺寸:(36.000000 36.000000)
修改圖片到指定的尺寸
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size {
if (CGSizeEqualToSize(self.size, size) || CGSizeEqualToSize(size, CGSizeZero)) {
return self;
}
CGSize scaledSize = size;
CGPoint thumbnailPoint = CGPointZero;
CGFloat widthFactor = size.width / self.size.width;
CGFloat heightFactor = size.height / self.size.height;
CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor;
scaledSize.width = self.size.width * scaleFactor;
scaledSize.height = self.size.height * scaleFactor;
if (widthFactor > heightFactor) {
thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
}
else if (widthFactor < heightFactor) {
thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
}
NSMutableArray *scaledImages = [NSMutableArray array];
for (UIImage *image in self.images) {
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
[scaledImages addObject:newImage];
UIGraphicsEndImageContext();
}
return [UIImage animatedImageWithImages:scaledImages duration:self.duration];
}
上邊的方法能夠實現把圖片的尺寸修剪為size,剪裁的前提是根據圖片原來的比例。具體的實現,在這里就不舉例說明了。和數學原理有點關系。
+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source
一個Image Sources抽象出來了圖片數據,通過raw memory buffer減輕開發人員對數據的處理。Image Sources包含不止一個圖像,縮略圖,各個圖像的特征和圖片文件。通過CGImageSource實現。可以這么說:
CGImageSourceRef就是對圖像數據的一層封裝。
+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
float frameDuration = 0.1f;
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
if (delayTimeUnclampedProp) {
frameDuration = [delayTimeUnclampedProp floatValue];
}
else {
NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
if (delayTimeProp) {
frameDuration = [delayTimeProp floatValue];
}
}
// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
// for more information.
if (frameDuration < 0.011f) {
frameDuration = 0.100f;
}
CFRelease(cfFrameProperties);
return frameDuration;
}
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data
當我們由NSData => UIImage 的時候,我們應該考慮更多一點。如果NSData中不止一張圖片,應該怎么辦?
-
獲取NSData中的圖片數量
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); size_t count = CGImageSourceGetCount(source);
-
如果圖片數量小於或者等於1,直接轉換
if (count <= 1) { animatedImage = [[UIImage alloc] initWithData:data]; }
-
數量大於1的情況
- 取出每一個圖片
- 計算總的duration
- 生成UIImage
代碼如下:
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {
if (!data) {
return nil;
}
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(source);
UIImage *animatedImage;
if (count <= 1) {
animatedImage = [[UIImage alloc] initWithData:data];
}
else {
NSMutableArray *images = [NSMutableArray array];
NSTimeInterval duration = 0.0f;
for (size_t i = 0; i < count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
if (!image) {
continue;
}
duration += [self sd_frameDurationAtIndex:i source:source];
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
CGImageRelease(image);
}
if (!duration) {
duration = (1.0f / 10.0f) * count;
}
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
}
CFRelease(source);
return animatedImage;
}
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name {
CGFloat scale = [UIScreen mainScreen].scale;
if (scale > 1.0f) {
NSString *retinaPath = [[NSBundle mainBundle] pathForResource:[name stringByAppendingString:@"@2x"] ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:retinaPath];
if (data) {
return [UIImage sd_animatedGIFWithData:data];
}
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
data = [NSData dataWithContentsOfFile:path];
if (data) {
return [UIImage sd_animatedGIFWithData:data];
}
return [UIImage imageNamed:name];
}
else {
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data) {
return [UIImage sd_animatedGIFWithData:data];
}
return [UIImage imageNamed:name];
}
}
補充
在這里補充一點實現漸進式圖片加載的步驟。
當圖片從網絡中獲取的時候,可能由於過大,數據緩慢,這時候就需要漸進式加載圖片來顯示。主要通過CFData對象來實現:
- 創建一個CFData去添加image data
- 創建一個漸進式圖片資源,通過 CGImageSourceCreateIncremental
- 獲取圖片數據到CFData中
- 調用CGImageSourceUpdateData函數,傳遞CFData和一個bool值,去描述這個數據是否包含全部圖片數據或者只是部分數據。無論什么情況,這個data包含已經積累的全部圖片文件
- 如果已經有足夠的圖片數據,可以通過函數繪制CGImageSourceCreateImageAtIndex部分圖片,然后記得要Release掉它
- 檢查是否已經有全部的圖片數據通過使用CGImageSourceGetStatusAtIndex函數。如果圖片是完整的,函數返回值為kCGImageStatusComplete。否則繼續3,4步驟,直到獲得全部數據
- Release掉漸進式增長的image source
總結
寫到這里,我突然意識到,gif也算是一種無損的格式,本分類也只是給予UIImage支持GIF的能力,因此由這種思想,我聯想到別的地方。當我們需要某種能力支持的時候,我們應該去觀察底層,也就是數據層的規律。就比如圖像數據,本質上還是一些二進制的數據,越往上,越被包裝的簡單易用,歸根到底,寫代碼的根本就是處理數據。