Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions iOS-WebP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ Pod::Spec.new do |s|
s.name = 'iOS-WebP'
s.platform = 'ios'
s.license = 'MIT'
s.version = '0.5'
s.version = '0.6'
s.homepage = 'https://github.com/seanooi/iOS-WebP'
s.summary = 'WebP image decoder and encoder for iOS'
s.author = { 'Sean Ooi' => 'sean.ooi@me.com' }
s.source = { :git => 'https://github.com/seanooi/iOS-WebP.git', :tag => '0.5' }
s.source = { :git => 'https://github.com/seanooi/iOS-WebP.git', :tag => '0.6' }
s.source_files = 'iOS-WebP/*.{h,m}'
s.requires_arc = true
s.dependency 'libwebp', '~> 0.5.0'
s.dependency 'libwebp', '~> 0.6.0'
end
2 changes: 2 additions & 0 deletions iOS-WebP/UIImage+WebP.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

+ (UIImage*)imageWithWebP:(NSString*)filePath;

+ (UIImage*)imageWithWebP:(NSString*)filePath scale:(CGFloat)scale;

+ (NSData*)imageToWebP:(UIImage*)image quality:(CGFloat)quality;

+ (void)imageToWebP:(UIImage*)image
Expand Down
48 changes: 41 additions & 7 deletions iOS-WebP/UIImage+WebP.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ + (NSData *)convertToWebP:(UIImage *)image
return webPFinalData;
}

+ (UIImage *)imageWithWebP:(NSString *)filePath error:(NSError **)error
+ (UIImage *)imageWithWebP:(NSString *)filePath scale:(CGFloat)scale error:(NSError **)error
{
// If passed `filepath` is invalid, return nil to caller and log error in console
NSError *dataError = nil;
Expand All @@ -113,10 +113,10 @@ + (UIImage *)imageWithWebP:(NSString *)filePath error:(NSError **)error
*error = dataError;
return nil;
}
return [UIImage imageWithWebPData:imgData error:error];
return [UIImage imageWithWebPData:imgData scale:scale error:error];
}

+ (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error
+ (UIImage *)imageWithWebPData:(NSData *)imgData scale:(CGFloat)scale error:(NSError **)error
{
// `WebPGetInfo` weill return image width and height
int width = 0, height = 0;
Expand Down Expand Up @@ -163,7 +163,7 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent);
UIImage *result = [UIImage imageWithCGImage:imageRef];
UIImage *result = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];

// Free resources to avoid memory leaks
CGImageRelease(imageRef);
Expand All @@ -173,17 +173,49 @@ + (UIImage *)imageWithWebPData:(NSData *)imgData error:(NSError **)error
return result;
}

+ (NSString *)filePathScaled:(NSString *)filePath scale:(CGFloat *)scale {
NSString *extension = [filePath pathExtension];
filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filePath];

while (*scale > 1) {
NSString *file = [[[filePath stringByDeletingPathExtension] stringByAppendingFormat:@"@%1.0ldx", (long) *scale] stringByAppendingPathExtension:extension];

if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
filePath = file;
break;
}

*scale -= 1;
}

if(*scale == 1 && ![[NSFileManager defaultManager] fileExistsAtPath:filePath])
return nil;

return filePath;
}

#pragma mark - Synchronous methods
+ (UIImage *)imageWithWebP:(NSString *)filePath
{
CGFloat scale = [[UIScreen mainScreen] scale];
filePath = [self filePathScaled:filePath scale:&scale];

if(!filePath)
return nil;

return [self imageWithWebP:filePath scale:scale];
}

+ (UIImage *)imageWithWebP:(NSString *)filePath scale:(CGFloat)scale
{
NSParameterAssert(filePath != nil);
return [self imageWithWebP:filePath error:nil];
return [self imageWithWebP:filePath scale:scale error:nil];
}

+ (UIImage *)imageWithWebPData:(NSData *)imgData
{
NSParameterAssert(imgData != nil);
return [self imageWithWebPData:imgData error:nil];
return [self imageWithWebPData:imgData scale:1.0f error:nil];
}

+ (NSData *)imageToWebP:(UIImage *)image quality:(CGFloat)quality
Expand All @@ -205,7 +237,9 @@ + (void)imageWithWebP:(NSString *)filePath completionBlock:(void (^)(UIImage *re
dispatch_async(fromWebPQueue, ^{

NSError *error = nil;
UIImage *webPImage = [self imageWithWebP:filePath error:&error];
CGFloat scale = [[UIScreen mainScreen] scale];

UIImage *webPImage = [self imageWithWebP:[self filePathScaled:filePath scale:&scale] scale:scale error:&error];

// Return results to caller on main thread in completion block if `webPImage` != nil
// Else return in failure block
Expand Down