GIF 裁剪過程可以大致分為三步
- 獲取 GIF 的幀集合,并對(duì)每一幀圖片進(jìn)行裁剪后生成裁剪后的 UIImage 對(duì)象
- 裁剪后的 GIF 的 NSData 數(shù)據(jù)
這里主要用到了 Image I/O 的相關(guān)接口儡循,Image I/O framework 提供一種不透明的數(shù)據(jù)類型(opaque data types)官还,從 CGImageSourceRef 獲取圖片數(shù)據(jù)堕虹,將圖片數(shù)據(jù)寫入到 CGImageDestinationRef鬼雀。它提供一個(gè)范圍很廣的圖片格式实撒,包含 web 格式靴患,動(dòng)態(tài)圖,原始相機(jī)數(shù)據(jù)等豁状。
獲取幀集合并裁剪
首先是獲取 GIF 的幀集合捉偏。
- 通過 GIF 的 NSData 創(chuàng)建一個(gè) CGImageSourceRef 對(duì)象
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)assetData, NULL);
這里 CGImageSourceRef 就是一個(gè)代表圖片的不透明的數(shù)據(jù)類型,它抽象了讀取圖像數(shù)據(jù)的通道泻红,但本身不會(huì)讀取圖像的任何數(shù)據(jù)夭禽。
- 獲取 GIF 幀的個(gè)數(shù)
size_t count = CGImageSourceGetCount(source);
對(duì)于 GIF 等 AnimateImage 對(duì)象可以包含多個(gè) Image,這里 CGImageSourceGetCount 就可以獲取到對(duì)應(yīng)的源圖片數(shù)據(jù)的幀數(shù)谊路。
- 獲取幀集合并裁剪
NSMutableArray *images = [NSMutableArray array];
NSTimeInterval duration = (1.0f / 100.0f) * count;
for (size_t i = 0; i < count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
CGImageRef newImageRef = CGImageCreateWithImageInRect(image, cropRect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
[images addObject:newImage];
CGImageRelease(image);
}
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
GIF 的一個(gè)重要屬性是它的 duration 值讹躯,這里簡單取為幀數(shù)的 10%,相當(dāng)于每一幀的 duration 是 0.1s缠劝。SDWebImage 庫的 SDWebImageGIFCoder.m 文件中提供了一種更準(zhǔn)確的獲取方法
- (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;
}
通過 CGImageSourceCreateImageAtIndex 方法我們可以從一個(gè) CGImageSourceRef 數(shù)據(jù)源中讀到一個(gè)幀位的未解碼圖片數(shù)據(jù)潮梯,然后通過 CGImageCreateWithImageInRect 對(duì)這一幀數(shù)據(jù)進(jìn)行裁剪后,解碼生成一個(gè) UIImage 對(duì)象剩彬,將其放入一個(gè)數(shù)組中酷麦。最后通過 [UIImage animatedImageWithImages: duration:] 方法來生成一個(gè)被裁剪后的 GIF 的 UIImage 對(duì)象。這時(shí)候 可以把這個(gè) UIImage 對(duì)象賦給 UIImageView 對(duì)象來展示一個(gè) GIF 圖片喉恋。
最后要注意手動(dòng)釋放 CGImageSourceRef 對(duì)象
CFRelease(source);
獲取裁剪后的 NSData
如果要傳輸裁剪后的 GIF 圖片給服務(wù)器,還需要將上一步得到的 UIImage 轉(zhuǎn)化為 NSData 對(duì)象。
size_t frameCount = animatedImage.images.count;
NSTimeInterval frameDuration = animatedImage.duration / frameCount;
NSDictionary *frameProperties = @{
(__bridge NSString *)kCGImagePropertyGIFDictionary: @{
(__bridge NSString *)kCGImagePropertyGIFDelayTime: @(frameDuration)
}
};
NSMutableData *mutableData = [NSMutableData data];
NSDictionary *imageProperties = @{ (__bridge NSString *)kCGImagePropertyGIFDictionary: @{
(__bridge NSString *)kCGImagePropertyGIFLoopCount: @0
}
};
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)mutableData, kUTTypeGIF, frameCount, NULL);
CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)imageProperties);
for (size_t index = 0; index < frameCount;index++) {
CGImageDestinationAddImage(destination, [[animatedImage.images objectAtIndex:index] CGImage], (__bridge CFDictionaryRef)frameProperties);
}
BOOL success = CGImageDestinationFinalize(destination);
CFRelease(destination);
這里通過上一步得到的 UIImage 對(duì)象轻黑,首先獲得 GIF 圖的幀數(shù)糊肤,然后獲得每一幀的 duration。同時(shí)創(chuàng)建一個(gè) NSMutableData 的對(duì)象用于存儲(chǔ) GIF 圖片的數(shù)據(jù)氓鄙,然后創(chuàng)建一個(gè) CGImageDestinationRef 來指定存入數(shù)據(jù)區(qū)(mutableData)馆揉、存入數(shù)據(jù)類型(kUTTypeGIF)和幀數(shù)(frameCount)。最后遍歷 animatedImage 的每一個(gè) UIImage 對(duì)象抖拦,依據(jù)每一幀的 duration 值存入 CGImageDestinationRef 對(duì)象中升酣。執(zhí)行完遍歷操作后,mutableData 中就是我們需要的裁剪后的 GIF 圖片的 NSData 數(shù)據(jù)态罪。