Skip to content

Commit

Permalink
feat: Add rotation to Image in iOS (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmartin8a authored Nov 2, 2023
1 parent 580bf51 commit 13cb50a
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ @implementation MLKVisionImage(FlutterPlugin)
+ (MLKVisionImage *)visionImageFromData:(NSDictionary *)imageData {
NSString *imageType = imageData[@"type"];
if ([@"file" isEqualToString:imageType]) {
return [self filePathToVisionImage:imageData[@"path"]];
int rotation = [imageData[@"metadata"][@"rotation"] intValue];
UIImageOrientation imageOrientation = [self imageOrientationFromRotation:rotation];
MLKVisionImage *image = [self filePathToVisionImage:imageData[@"path"]];
image.orientation = imageOrientation;
return image;
} else if ([@"bytes" isEqualToString:imageType]) {
return [self bytesToVisionImage:imageData];
int rotation = [imageData[@"metadata"][@"rotation"] intValue];
UIImageOrientation imageOrientation = [self imageOrientationFromRotation:rotation];
MLKVisionImage *image = [self bytesToVisionImage:imageData];
image.orientation = imageOrientation;
return image;
} else {
NSString *errorReason = [NSString stringWithFormat:@"No image type for: %@", imageType];
@throw [NSException exceptionWithName:NSInvalidArgumentException
Expand All @@ -17,6 +25,19 @@ + (MLKVisionImage *)visionImageFromData:(NSDictionary *)imageData {
}
}

+ (UIImageOrientation)imageOrientationFromRotation:(int)rotation {
switch (rotation) {
case 90:
return UIImageOrientationRight; // Rotates the image 90 degrees to the right
case 180:
return UIImageOrientationDown; // Rotates the image 180 degrees
case 270:
return UIImageOrientationLeft; // Rotates the image 90 degrees to the left
default:
return UIImageOrientationUp; // Default orientation (no rotation)
}
}

+ (MLKVisionImage *)filePathToVisionImage:(NSString *)filePath {
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
Expand Down

0 comments on commit 13cb50a

Please sign in to comment.