Skip to content

Commit ead68a0

Browse files
authoredJan 28, 2019
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
·
0.14.60.1.2
2 parents 97ff5b0 + dbcd5b2 commit ead68a0

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed
 

‎SDWebImageWebPCoder/Classes/SDImageWebPCoder.m

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,13 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
464464
BOOL encodeFirstFrame = [options[SDImageCoderEncodeFirstFrameOnly] boolValue];
465465
if (encodeFirstFrame || frames.count == 0) {
466466
// for static single webp image
467-
data = [self sd_encodedWebpDataWithImage:image quality:compressionQuality];
467+
CGImageRef imageRef = image.CGImage;
468+
#if SD_UIKIT || SD_WATCH
469+
if (!imageRef) {
470+
imageRef = image.images.firstObject.CGImage;
471+
}
472+
#endif
473+
data = [self sd_encodedWebpDataWithImage:imageRef quality:compressionQuality];
468474
} else {
469475
// for animated webp image
470476
WebPMux *mux = WebPMuxNew();
@@ -473,7 +479,7 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
473479
}
474480
for (size_t i = 0; i < frames.count; i++) {
475481
SDImageFrame *currentFrame = frames[i];
476-
NSData *webpData = [self sd_encodedWebpDataWithImage:currentFrame.image quality:compressionQuality];
482+
NSData *webpData = [self sd_encodedWebpDataWithImage:currentFrame.image.CGImage quality:compressionQuality];
477483
int duration = currentFrame.duration * 1000;
478484
WebPMuxFrameInfo frame = { .bitstream.bytes = webpData.bytes,
479485
.bitstream.size = webpData.length,
@@ -510,14 +516,12 @@ - (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format o
510516
return data;
511517
}
512518

513-
- (nullable NSData *)sd_encodedWebpDataWithImage:(nullable UIImage *)image quality:(double)quality {
514-
if (!image) {
519+
- (nullable NSData *)sd_encodedWebpDataWithImage:(nullable CGImageRef)imageRef quality:(double)quality {
520+
NSData *webpData;
521+
if (!imageRef) {
515522
return nil;
516523
}
517524

518-
NSData *webpData;
519-
CGImageRef imageRef = image.CGImage;
520-
521525
size_t width = CGImageGetWidth(imageRef);
522526
size_t height = CGImageGetHeight(imageRef);
523527
if (width == 0 || width > WEBP_MAX_DIMENSION) {

0 commit comments

Comments
 (0)
Please sign in to comment.