Skip to content

Commit

Permalink
Merge pull request #4 from spoonconsulting/fix-thumbnail-size-issue
Browse files Browse the repository at this point in the history
create thumbnail with appropriate height and width
  • Loading branch information
dinitri authored Jun 9, 2023
2 parents baba057 + eda77b8 commit 3dc41c9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spoonconsulting/cordova-plugin-thumbnail",
"version": "1.0.2",
"version": "1.0.3",
"description": "Image thumbnail generator for Cordova project",
"cordova": {
"id": "@spoonconsulting/cordova-plugin-thumbnail",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="@spoonconsulting/cordova-plugin-thumbnail" version="1.0.2">
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android" id="@spoonconsulting/cordova-plugin-thumbnail" version="1.0.3">
<name>Thumbnail</name>
<description>Cordova Thumbnail Plugin</description>
<license>Apache 2.0</license>
Expand Down
46 changes: 28 additions & 18 deletions src/ios/Thumbnail.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,35 @@ + (void) thumbnail:(NSString *)imageURL size:(CGFloat)maxSize toURL:(NSString *)

+ (UIImage *)thumbnailWithContentsOfURL:(NSURL *)URL maxPixelSize:(CGFloat)maxPixelSize
{
CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)URL, NULL);
if(imageSource == NULL) {
NSLog(@"Can not read from source: %@", URL);
return NULL;
NSData *data = [NSData dataWithContentsOfURL:URL];
UIImage* image = [[UIImage alloc] initWithData:data];
double thumbnailHeight;
double thumbnailWidth;
CGSize thumbnailSize;
double maxPointSize = maxPixelSize / (double) (image.scale);

double ratio;
if ((image.size.width / image.size.height) > 1) {
ratio = MAX(image.size.width / maxPointSize, image.size.height / maxPointSize);
} else {
ratio = MIN(image.size.width / maxPointSize, image.size.height / maxPointSize);
}

if (@available(iOS 15.0, *)) {
thumbnailHeight = (image.size.height / ratio) / image.scale;
thumbnailWidth = (image.size.width / ratio) / image.scale;
thumbnailSize = CGSizeMake(thumbnailWidth, thumbnailHeight);
return [image imageByPreparingThumbnailOfSize: thumbnailSize];
} else {
thumbnailHeight = (image.size.height / ratio) / UIScreen.mainScreen.scale;
thumbnailWidth = (image.size.width / ratio) / UIScreen.mainScreen.scale;
thumbnailSize = CGSizeMake(thumbnailWidth, thumbnailHeight);
UIGraphicsBeginImageContextWithOptions(thumbnailSize, false, 0.0);
[image drawInRect:CGRectMake(0, 0, thumbnailSize.width, thumbnailSize.height)];
UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return thumbnailImage;
}

NSDictionary *imageOptions = @{
(NSString const *)kCGImageSourceCreateThumbnailFromImageIfAbsent : (NSNumber const *)kCFBooleanTrue,
(NSString const *)kCGImageSourceThumbnailMaxPixelSize : @(maxPixelSize),
(NSString const *)kCGImageSourceCreateThumbnailWithTransform : (NSNumber const *)kCFBooleanTrue,
(NSString const *)kCGImageSourceCreateThumbnailFromImageAlways : (NSNumber const *)kCFBooleanTrue
};
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (__bridge CFDictionaryRef)imageOptions);
CFRelease(imageSource);

UIImage *result = [[UIImage alloc] initWithCGImage:thumbnail];
CGImageRelease(thumbnail);

return result;
}

@end
Expand Down

0 comments on commit 3dc41c9

Please sign in to comment.