Merge pull request #9 from SDWebImage/bugfix_encoding_first_frame

Fix the issue when provide a `UIAnimatedImage` and encode it into a static WebP failed
This commit is contained in:
DreamPiggy 2019-01-28 17:21:42 +08:00 committed by GitHub
commit ead68a0e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 7 deletions

View File

@ -464,7 +464,13 @@
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
if (encodeFirstFrame || frames.count == 0) {
// 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 {
// for animated webp image
WebPMux *mux = WebPMuxNew();
@ -473,7 +479,7 @@
}
for (size_t i = 0; i < frames.count; 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;
WebPMuxFrameInfo frame = { .bitstream.bytes = webpData.bytes,
.bitstream.size = webpData.length,
@ -510,14 +516,12 @@
return data;
}
- (nullable NSData *)sd_encodedWebpDataWithImage:(nullable UIImage *)image quality:(double)quality {
if (!image) {
- (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef quality:(double)quality {
NSData *webpData;
if (!imageRef) {
return nil;
}
NSData *webpData;
CGImageRef imageRef = image.CGImage;
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
if (width == 0 || width > WEBP_MAX_DIMENSION) {