diff --git a/SDWebImage/UIImage+GIF.h b/SDWebImage/UIImage+GIF.h index 1062a291..38b986be 100644 --- a/SDWebImage/UIImage+GIF.h +++ b/SDWebImage/UIImage+GIF.h @@ -9,24 +9,16 @@ #import "SDWebImageCompat.h" +// This category is just use as a convenience method. For more detail control, use methods in `UIImage+MultiFormat.h` or directlly use `SDImageCoder` @interface UIImage (GIF) /** Creates an animated UIImage from an NSData. - For Static GIF, will create an UIImage with `images` array set to nil. For Animated GIF, will create an UIImage with valid `images` array. + This will create animated image if the data is Animated GIF. And will create a static image is the data is Static GIF. @param data The GIF data @return The created image */ -+ (nullable UIImage *)sd_animatedGIFWithData:(nullable NSData *)data; - -/** - Creates an animated UIImage from an NSData. - - @param data The GIF data - @param firstFrameOnly Even if the image data is Animated GIF format, decode the first frame only - @return The created image - */ -+ (nullable UIImage *)sd_animatedGIFWithData:(nullable NSData *)data firstFrameOnly:(BOOL)firstFrameOnly; ++ (nullable UIImage *)sd_imageWithGIFData:(nullable NSData *)data; @end diff --git a/SDWebImage/UIImage+GIF.m b/SDWebImage/UIImage+GIF.m index d2672721..7158cf31 100644 --- a/SDWebImage/UIImage+GIF.m +++ b/SDWebImage/UIImage+GIF.m @@ -12,16 +12,11 @@ @implementation UIImage (GIF) -+ (nullable UIImage *)sd_animatedGIFWithData:(nullable NSData *)data { - return [self sd_animatedGIFWithData:data firstFrameOnly:NO]; -} - -+ (nullable UIImage *)sd_animatedGIFWithData:(nullable NSData *)data firstFrameOnly:(BOOL)firstFrameOnly { ++ (nullable UIImage *)sd_imageWithGIFData:(nullable NSData *)data { if (!data) { return nil; } - SDImageCoderOptions *options = @{SDImageCoderDecodeFirstFrameOnly : @(firstFrameOnly)}; - return [[SDImageGIFCoder sharedCoder] decodedImageWithData:data options:options]; + return [[SDImageGIFCoder sharedCoder] decodedImageWithData:data options:0]; } @end diff --git a/SDWebImage/UIImage+MultiFormat.h b/SDWebImage/UIImage+MultiFormat.h index ff4b9ee8..951d66a5 100644 --- a/SDWebImage/UIImage+MultiFormat.h +++ b/SDWebImage/UIImage+MultiFormat.h @@ -28,6 +28,16 @@ */ + (nullable UIImage *)sd_imageWithData:(nullable NSData *)data scale:(CGFloat)scale; +/** + Create and decode a image with the specify image data and scale, allow specify animate/static control + + @param data The image data + @param scale The image scale factor. Should be greater than or equal to 1.0. + @param firstFrameOnly Even if the image data is animated image format, decode the first frame only as static image. + @return The created image + */ ++ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data scale:(CGFloat)scale firstFrameOnly:(BOOL)firstFrameOnly; + #pragma mark - Encode /** Encode the current image to the data, the image format is unspecified @@ -53,4 +63,14 @@ */ - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality; +/** + Encode the current image to data with the specify image format and compression quality, allow specify animate/static control + + @param imageFormat The specify image format + @param compressionQuality The quality of the resulting image data. Value between 0.0-1.0. Some coders may not support compression quality. + @param firstFrameOnly Even if the image is animated image, encode the first frame only as static image. + @return The encoded data. If can't encode, return nil + */ +- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality firstFrameOnly:(BOOL)firstFrameOnly; + @end diff --git a/SDWebImage/UIImage+MultiFormat.m b/SDWebImage/UIImage+MultiFormat.m index 0e59b18d..c70a3808 100644 --- a/SDWebImage/UIImage+MultiFormat.m +++ b/SDWebImage/UIImage+MultiFormat.m @@ -16,13 +16,17 @@ } + (nullable UIImage *)sd_imageWithData:(nullable NSData *)data scale:(CGFloat)scale { + return [self sd_imageWithData:data scale:scale firstFrameOnly:NO]; +} + ++ (nullable UIImage *)sd_imageWithData:(nullable NSData *)data scale:(CGFloat)scale firstFrameOnly:(BOOL)firstFrameOnly { if (!data) { return nil; } if (scale < 1) { scale = 1; } - SDImageCoderOptions *options = @{SDImageCoderDecodeScaleFactor : @(scale)}; + SDImageCoderOptions *options = @{SDImageCoderDecodeScaleFactor : @(scale), SDImageCoderDecodeFirstFrameOnly : @(firstFrameOnly)}; return [[SDImageCodersManager sharedManager] decodedImageWithData:data options:options]; } @@ -35,7 +39,11 @@ } - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality { - SDImageCoderOptions *options = @{SDImageCoderEncodeCompressionQuality : @(compressionQuality)}; + return [self sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:NO]; +} + +- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality firstFrameOnly:(BOOL)firstFrameOnly { + SDImageCoderOptions *options = @{SDImageCoderEncodeCompressionQuality : @(compressionQuality), SDImageCoderEncodeFirstFrameOnly : @(firstFrameOnly)}; return [[SDImageCodersManager sharedManager] encodedDataWithImage:self format:imageFormat options:options]; } diff --git a/SDWebImage/WebP/UIImage+WebP.h b/SDWebImage/WebP/UIImage+WebP.h index 7f4f50d4..b799b53c 100644 --- a/SDWebImage/WebP/UIImage+WebP.h +++ b/SDWebImage/WebP/UIImage+WebP.h @@ -10,26 +10,18 @@ #import "SDWebImageCompat.h" +// This category is just use as a convenience method. For more detail control, use methods in `UIImage+MultiFormat.h` or directlly use `SDImageCoder` @interface UIImage (WebP) /** Create a image from the WebP data. - This may create animated image if the data is Animated WebP. + This will create animated image if the data is Animated WebP. And will create a static image is the data is Static WebP. @param data The WebP data @return The created image */ + (nullable UIImage *)sd_imageWithWebPData:(nullable NSData *)data; -/** - Create a image from the WebP data. - - @param data The WebP data - @param firstFrameOnly Even if the image data is Animated WebP format, decode the first frame only - @return The created image - */ -+ (nullable UIImage *)sd_imageWithWebPData:(nullable NSData *)data firstFrameOnly:(BOOL)firstFrameOnly; - @end #endif diff --git a/SDWebImage/WebP/UIImage+WebP.m b/SDWebImage/WebP/UIImage+WebP.m index 9fe69554..011b8ad2 100644 --- a/SDWebImage/WebP/UIImage+WebP.m +++ b/SDWebImage/WebP/UIImage+WebP.m @@ -14,15 +14,10 @@ @implementation UIImage (WebP) + (nullable UIImage *)sd_imageWithWebPData:(nullable NSData *)data { - return [self sd_imageWithWebPData:data firstFrameOnly:NO]; -} - -+ (nullable UIImage *)sd_imageWithWebPData:(nullable NSData *)data firstFrameOnly:(BOOL)firstFrameOnly { if (!data) { return nil; } - SDImageCoderOptions *options = @{SDImageCoderDecodeFirstFrameOnly : @(firstFrameOnly)}; - return [[SDImageWebPCoder sharedCoder] decodedImageWithData:data options:options]; + return [[SDImageWebPCoder sharedCoder] decodedImageWithData:data options:0]; } @end diff --git a/Tests/Tests/SDCategoriesTests.m b/Tests/Tests/SDCategoriesTests.m index c8429284..36996529 100644 --- a/Tests/Tests/SDCategoriesTests.m +++ b/Tests/Tests/SDCategoriesTests.m @@ -43,11 +43,11 @@ - (void)test03UIImageGIFCategory { // Test invalid image data - UIImage *image = [UIImage sd_animatedGIFWithData:nil]; + UIImage *image = [UIImage sd_imageWithGIFData:nil]; expect(image).to.beNil(); // Test valid image data NSData *data = [NSData dataWithContentsOfFile:[self testGIFPath]]; - image = [UIImage sd_animatedGIFWithData:data]; + image = [UIImage sd_imageWithGIFData:data]; expect(image).notTo.beNil(); }