Skip to content

Commit

Permalink
Fixed LoadImageAtPath sometimes not loading GIF and HEIC images corre…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
yasirkula committed Oct 14, 2019
1 parent 00b486e commit 298d78a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions Plugins/NativeCamera/iOS/NativeCamera.mm
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,30 @@ + (UIImage *)scaleImage:(UIImage *)image maxSize:(int)maxSize {
}

+ (char *)loadImageAtPath:(NSString *)path tempFilePath:(NSString *)tempFilePath maximumSize:(int)maximumSize {
NSArray *metadata = [self getImageMetadata:path];
int orientationInt = [metadata[2] intValue]; // 1: correct orientation, [1,8]: valid orientation range
if (( orientationInt <= 1 || orientationInt > 8 ) && [metadata[0] intValue] <= maximumSize && [metadata[1] intValue] <= maximumSize)
return [self getCString:path];
// Check if the image can be loaded by Unity without requiring a conversion to PNG
// Credit: https://stackoverflow.com/a/12048937/2373034
NSString *extension = [path pathExtension];
BOOL conversionNeeded = [extension caseInsensitiveCompare:@"jpg"] != NSOrderedSame && [extension caseInsensitiveCompare:@"jpeg"] != NSOrderedSame && [extension caseInsensitiveCompare:@"png"] != NSOrderedSame;

if (!conversionNeeded) {
// Check if the image needs to be processed at all
NSArray *metadata = [self getImageMetadata:path];
int orientationInt = [metadata[2] intValue]; // 1: correct orientation, [1,8]: valid orientation range
if (( orientationInt <= 1 || orientationInt > 8 ) && [metadata[0] intValue] <= maximumSize && [metadata[1] intValue] <= maximumSize)
return [self getCString:path];
}

UIImage *image = [UIImage imageWithContentsOfFile:path];
if (image == nil)
return [self getCString:path];

UIImage *scaledImage = [self scaleImage:image maxSize:maximumSize];
if (scaledImage != image) {
[UIImagePNGRepresentation(scaledImage) writeToFile:tempFilePath atomically:YES];
if (conversionNeeded || scaledImage != image) {
if (![UIImagePNGRepresentation(scaledImage) writeToFile:tempFilePath atomically:YES]) {
NSLog(@"Error creating scaled image");
return [self getCString:path];
}

return [self getCString:tempFilePath];
}
else
Expand Down

0 comments on commit 298d78a

Please sign in to comment.