173 lines
5.7 KiB
Objective-C
Executable File
173 lines
5.7 KiB
Objective-C
Executable File
//
|
||
// UIImage+Additions.m
|
||
// GoldCow
|
||
//
|
||
// Created by haiwei on 15/6/1.
|
||
// Copyright (c) 2015年 ddsfsdfsd. All rights reserved.
|
||
//
|
||
|
||
#import "UIImage+Additions.h"
|
||
|
||
#import <ImageIO/ImageIO.h>
|
||
|
||
@implementation UIImage (Additions)
|
||
|
||
+ (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size {
|
||
|
||
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
|
||
UIGraphicsBeginImageContext(size);
|
||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||
|
||
CGContextSetFillColorWithColor(context, [color CGColor]);
|
||
CGContextFillRect(context, rect);
|
||
|
||
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
|
||
UIGraphicsEndImageContext();
|
||
|
||
return image;
|
||
}
|
||
|
||
+ (UIImage *)imageWithColor:(UIColor *)color {
|
||
return [UIImage imageWithColor:color withSize:CGSizeMake(1, 1)];
|
||
}
|
||
|
||
|
||
- (UIImage *)imageByCropToRect:(CGRect)rect {
|
||
rect.origin.x *= self.scale;
|
||
rect.origin.y *= self.scale;
|
||
rect.size.width *= self.scale;
|
||
rect.size.height *= self.scale;
|
||
if (rect.size.width <= 0 || rect.size.height <= 0) return nil;
|
||
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
|
||
UIImage *image = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
|
||
CGImageRelease(imageRef);
|
||
return image;
|
||
}
|
||
|
||
+ (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 animatedGIFWithData:data];
|
||
}
|
||
|
||
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
|
||
|
||
data = [NSData dataWithContentsOfFile:path];
|
||
|
||
if (data) {
|
||
return [UIImage animatedGIFWithData:data];
|
||
}
|
||
|
||
return [UIImage imageNamed:name];
|
||
}
|
||
else {
|
||
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"gif"];
|
||
|
||
NSData *data = [NSData dataWithContentsOfFile:path];
|
||
|
||
if (data) {
|
||
return [UIImage animatedGIFWithData:data];
|
||
}
|
||
|
||
return [UIImage imageNamed:name];
|
||
}
|
||
}
|
||
|
||
+ (UIImage *)animatedGIFWithData:(NSData *)data {
|
||
if (!data) {
|
||
return nil;
|
||
}
|
||
|
||
//获取数据源
|
||
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||
|
||
// 获取图片数量(如果传入的是gif图的二进制,那么获取的是图片帧数)
|
||
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);
|
||
|
||
duration += [UIImage frameDurationAtIndex:i source:source];
|
||
|
||
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
|
||
|
||
CGImageRelease(image);
|
||
}
|
||
// 如果上面的计算播放时间方法没有成功,就按照下面方法计算
|
||
// 计算一次播放的总时间:每张图播放1/10秒 * 图片总数
|
||
if (!duration) {
|
||
duration = (1.0f / 10.0f) * count;
|
||
}
|
||
|
||
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
|
||
}
|
||
|
||
CFRelease(source);
|
||
|
||
return animatedImage;
|
||
}
|
||
|
||
+ (float)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 *)xy_getVideoThumbnail:(NSString *)filePath
|
||
{
|
||
NSURL *sourceURL = [NSURL fileURLWithPath:filePath];
|
||
AVAsset *asset = [AVAsset assetWithURL:sourceURL];
|
||
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
|
||
imageGenerator.appliesPreferredTrackTransform = YES;
|
||
CMTime time = CMTimeMake(0, 1);
|
||
NSError *error;
|
||
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:&error];
|
||
UIImage *thumbnail = [UIImage imageWithCGImage:imageRef];
|
||
CGImageRelease(imageRef); // CGImageRef won't be released by ARC
|
||
return thumbnail;
|
||
}
|
||
@end
|