Fix the issue when provide a `UIAnimatedImage` and encode it into a static WebP failed. Add a backup check of image.images

This commit is contained in:
DreamPiggy 2019-01-28 14:50:34 +08:00
parent 97ff5b05da
commit dbcd5b27d0
1 changed files with 11 additions and 7 deletions

View File

@ -464,7 +464,13 @@
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue]; BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
if (encodeFirstFrame || frames.count == 0) { if (encodeFirstFrame || frames.count == 0) {
// for static single webp image // for static single webp image
data = [self sd_encodedWebpDataWithImage:image quality:compressionQuality]; CGImageRef imageRef = image.CGImage;
#if SD_UIKIT || SD_WATCH
if (!imageRef) {
imageRef = image.images.firstObject.CGImage;
}
#endif
data = [self sd_encodedWebpDataWithImage:imageRef quality:compressionQuality];
} else { } else {
// for animated webp image // for animated webp image
WebPMux *mux = WebPMuxNew(); WebPMux *mux = WebPMuxNew();
@ -473,7 +479,7 @@
} }
for (size_t i = 0; i < frames.count; i++) { for (size_t i = 0; i < frames.count; i++) {
SDImageFrame *currentFrame = frames[i]; SDImageFrame *currentFrame = frames[i];
NSData *webpData = [self sd_encodedWebpDataWithImage:currentFrame.image quality:compressionQuality]; NSData *webpData = [self sd_encodedWebpDataWithImage:currentFrame.image.CGImage quality:compressionQuality];
int duration = currentFrame.duration * 1000; int duration = currentFrame.duration * 1000;
WebPMuxFrameInfo frame = { .bitstream.bytes = webpData.bytes, WebPMuxFrameInfo frame = { .bitstream.bytes = webpData.bytes,
.bitstream.size = webpData.length, .bitstream.size = webpData.length,
@ -510,14 +516,12 @@
return data; return data;
} }
- (nullable NSData *)sd_encodedWebpDataWithImage:(nullable UIImage *)image quality:(double)quality { - (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef quality:(double)quality {
if (!image) { NSData *webpData;
if (!imageRef) {
return nil; return nil;
} }
NSData *webpData;
CGImageRef imageRef = image.CGImage;
size_t width = CGImageGetWidth(imageRef); size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef); size_t height = CGImageGetHeight(imageRef);
if (width == 0 || width > WEBP_MAX_DIMENSION) { if (width == 0 || width > WEBP_MAX_DIMENSION) {