Add basic support for rotation on iOS (#46)
This commit is contained in:
parent
8bb52ccf8d
commit
af2a09e1d8
|
@ -53,7 +53,7 @@ maxWidth | Image max width (ratio is preserved)
|
||||||
maxHeight | Image max height (ratio is preserved)
|
maxHeight | Image max height (ratio is preserved)
|
||||||
compressFormat | Can be either JPEG, PNG or WEBP (android only).
|
compressFormat | Can be either JPEG, PNG or WEBP (android only).
|
||||||
quality | A number between 0 and 100. Used for the JPEG compression.
|
quality | A number between 0 and 100. Used for the JPEG compression.
|
||||||
rotation | Rotation to apply to the image, in degrees, for android only. On iOS, the resizing is done such that the orientation is always up.
|
rotation | Rotation to apply to the image, in degrees, for android. On iOS, rotation is limited (and rounded) to multiples of 90 degrees.
|
||||||
outputPath | The resized image path. If null, resized image will be stored in cache folder. To set outputPath make sure to add option for rotation too (if no rotation is needed, just set it to 0).
|
outputPath | The resized image path. If null, resized image will be stored in cache folder. To set outputPath make sure to add option for rotation too (if no rotation is needed, just set it to 0).
|
||||||
|
|
||||||
## Other open-source modules by the folks at [BAM](http://github.com/bamlab)
|
## Other open-source modules by the folks at [BAM](http://github.com/bamlab)
|
||||||
|
|
|
@ -9,7 +9,7 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
NativeModules.ImageResizer.createResizedImage(path, width, height, format, quality, outputPath, (err, resizedPath) => {
|
NativeModules.ImageResizer.createResizedImage(path, width, height, format, quality, rotation, outputPath, (err, resizedPath) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,11 +51,45 @@ NSString * generateFilePath(NSString * ext, NSString * outputPath)
|
||||||
return fullPath;
|
return fullPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UIImage * rotateImage(UIImage *inputImage, float rotationDegrees)
|
||||||
|
{
|
||||||
|
|
||||||
|
// We want only fixed 0, 90, 180, 270 degree rotations.
|
||||||
|
const int rotDiv90 = (int)round(rotationDegrees / 90);
|
||||||
|
const int rotQuadrant = rotDiv90 % 4;
|
||||||
|
const int rotQuadrantAbs = (rotQuadrant < 0) ? rotQuadrant + 4 : rotQuadrant;
|
||||||
|
|
||||||
|
// Return the input image if no rotation specified.
|
||||||
|
if (0 == rotQuadrantAbs) {
|
||||||
|
return inputImage;
|
||||||
|
} else {
|
||||||
|
// Rotate the image by 80, 180, 270.
|
||||||
|
UIImageOrientation orientation = UIImageOrientationUp;
|
||||||
|
|
||||||
|
switch(rotQuadrantAbs) {
|
||||||
|
case 1:
|
||||||
|
orientation = UIImageOrientationRight; // 90 deg CW
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
orientation = UIImageOrientationDown; // 180 deg rotation
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
orientation = UIImageOrientationLeft; // 90 deg CCW
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [[UIImage alloc] initWithCGImage: inputImage.CGImage
|
||||||
|
scale: 1.0
|
||||||
|
orientation: orientation];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
||||||
width:(float)width
|
width:(float)width
|
||||||
height:(float)height
|
height:(float)height
|
||||||
format:(NSString *)format
|
format:(NSString *)format
|
||||||
quality:(float)quality
|
quality:(float)quality
|
||||||
|
rotation:(float)rotation
|
||||||
outputPath:(NSString *)outputPath
|
outputPath:(NSString *)outputPath
|
||||||
callback:(RCTResponseSenderBlock)callback)
|
callback:(RCTResponseSenderBlock)callback)
|
||||||
{
|
{
|
||||||
|
@ -76,6 +110,15 @@ RCT_EXPORT_METHOD(createResizedImage:(NSString *)path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Rotate image if rotation is specified.
|
||||||
|
if (0 != (int)rotation) {
|
||||||
|
image = rotateImage(image, rotation);
|
||||||
|
if (image == nil) {
|
||||||
|
callback(@[@"Can't rotate the image.", @""]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Do the resizing
|
// Do the resizing
|
||||||
UIImage * scaledImage = [image scaleToSize:newSize];
|
UIImage * scaledImage = [image scaleToSize:newSize];
|
||||||
if (scaledImage == nil) {
|
if (scaledImage == nil) {
|
||||||
|
|
Loading…
Reference in New Issue