第二篇html
本篇是和GIF相关的一个UIImage的分类。主要提供了三个方法:web
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name
----- 根据名称获取图片+ (UIImage *)sd_animatedGIFWithData:(NSData *)data
----- 根据NSData获取图片- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size
----- 修改图片到指定的尺寸咱们先无论图片的更高级的知识,咱们简单的对size和scale这两个属性作一下介绍。网络
注意:若是要获取一个图片的尺寸,不是直接使用image.size
,而是使用image.size
*image.scale
。固然,这是伪代码。缘由就是咱们在获取size的时候。使用的是Point坐标,而图片的尺寸是以像素为参照的。系统为咱们处理了这两种坐标系的转换工做。函数
咱们用一个例子来演示上边的内容:ui
UIImage *image = [UIImage imageNamed:@"photo_delete"]; NSLog(@"-----尺寸:(%f %f)", image.size.width, image.size.height);
打印结果为:code
-----尺寸:(18.000000 18.000000)
能够看出来。使用size这个属性是不对的。该图片的实际尺寸为:
orm
那咱们修改下代码:htm
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,剪裁的前提是根据图片原来的比例。具体的实现,在这里就不举例说明了。和数学原理有点关系。
blog
一个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; }
当咱们由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的状况
代码以下:
+ (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 { 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对象来实现:
写到这里,我忽然意识到,gif也算是一种无损的格式,本分类也只是给予UIImage支持GIF的能力,所以由这种思想,我联想到别的地方。当咱们须要某种能力支持的时候,咱们应该去观察底层,也就是数据层的规律。就好比图像数据,本质上仍是一些二进制的数据,越往上,越被包装的简单易用,归根到底,写代码的根本就是处理数据。