Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add rotation to ios #540

Merged
merged 7 commits into from
Nov 2, 2023
Merged
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
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