Merge pull request #38 from wix/image_params_fixes
Fixed size, width and height params on iOS for resized images. Added …
This commit is contained in:
commit
bda944ff01
@ -95,12 +95,14 @@ public class SaveImageTask extends AsyncTask<byte[], Void, Void> {
|
||||
return null;
|
||||
}
|
||||
|
||||
private WritableMap createImageInfo(String filePath, String id, String fileName, long fileSize) {
|
||||
private WritableMap createImageInfo(String filePath, String id, String fileName, long fileSize, int width, int height) {
|
||||
WritableMap imageInfo = Arguments.createMap();
|
||||
imageInfo.putString("uri", filePath);
|
||||
imageInfo.putString("id", id);
|
||||
imageInfo.putString("name", fileName);
|
||||
imageInfo.putInt("size", (int) fileSize);
|
||||
imageInfo.putInt("width", width);
|
||||
imageInfo.putInt("height", height);
|
||||
return imageInfo;
|
||||
}
|
||||
|
||||
@ -119,7 +121,7 @@ public class SaveImageTask extends AsyncTask<byte[], Void, Void> {
|
||||
long fileSize = new File(filePath).length();
|
||||
cursor.close();
|
||||
|
||||
return createImageInfo(filePath, filePath, fileName, fileSize);
|
||||
return createImageInfo(filePath, filePath, fileName, fileSize, image.getWidth(), image.getHeight());
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
@ -230,6 +232,6 @@ public class SaveImageTask extends AsyncTask<byte[], Void, Void> {
|
||||
Log.d(TAG, "Error accessing file: " + e.getMessage());
|
||||
imageFile = null;
|
||||
}
|
||||
return (imageFile != null) ? createImageInfo(Uri.fromFile(imageFile).toString(), imageFile.getAbsolutePath(), fileName, imageFile.length()) : null;
|
||||
return (imageFile != null) ? createImageInfo(Uri.fromFile(imageFile).toString(), imageFile.getAbsolutePath(), fileName, imageFile.length(), image.getWidth(), image.getHeight()) : null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
269292831D3B7D6000E07DDF /* CKCameraOverlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 269292821D3B7D6000E07DDF /* CKCameraOverlayView.m */; };
|
||||
269292861D3B81C800E07DDF /* CKOverlayObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 269292851D3B81C800E07DDF /* CKOverlayObject.m */; };
|
||||
26F556721D2501C9007B1C11 /* GalleryData.m in Sources */ = {isa = PBXBuildFile; fileRef = 26F556711D2501C9007B1C11 /* GalleryData.m */; };
|
||||
A7686BFE1EC9CFEC00959216 /* CKCompressedImage.m in Sources */ = {isa = PBXBuildFile; fileRef = A7686BFD1EC9CFEC00959216 /* CKCompressedImage.m */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
@ -53,6 +54,8 @@
|
||||
269292851D3B81C800E07DDF /* CKOverlayObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CKOverlayObject.m; sourceTree = "<group>"; };
|
||||
26F556701D2501C9007B1C11 /* GalleryData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GalleryData.h; sourceTree = "<group>"; };
|
||||
26F556711D2501C9007B1C11 /* GalleryData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GalleryData.m; sourceTree = "<group>"; };
|
||||
A7686BFC1EC9CFEC00959216 /* CKCompressedImage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CKCompressedImage.h; sourceTree = "<group>"; };
|
||||
A7686BFD1EC9CFEC00959216 /* CKCompressedImage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CKCompressedImage.m; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@ -87,6 +90,8 @@
|
||||
children = (
|
||||
26550AE41CFC2437007FF2DF /* CKGalleryManager.h */,
|
||||
26550AE51CFC2437007FF2DF /* CKGalleryManager.m */,
|
||||
A7686BFC1EC9CFEC00959216 /* CKCompressedImage.h */,
|
||||
A7686BFD1EC9CFEC00959216 /* CKCompressedImage.m */,
|
||||
26550AF41CFC7086007FF2DF /* CKCameraManager.h */,
|
||||
26550AF51CFC7086007FF2DF /* CKCameraManager.m */,
|
||||
2685AA221CFD89A300E4A446 /* CKCamera.h */,
|
||||
@ -166,6 +171,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
26550AF61CFC7086007FF2DF /* CKCameraManager.m in Sources */,
|
||||
A7686BFE1EC9CFEC00959216 /* CKCompressedImage.m in Sources */,
|
||||
262E42201D183A6B00C82B27 /* CKGalleryCollectionViewCell.m in Sources */,
|
||||
262564901E6572B300A7B90D /* CKGalleryCustomCollectionViewCell.m in Sources */,
|
||||
269292861D3B81C800E07DDF /* CKOverlayObject.m in Sources */,
|
||||
|
||||
18
ios/lib/ReactNativeCameraKit/CKCompressedImage.h
Normal file
18
ios/lib/ReactNativeCameraKit/CKCompressedImage.h
Normal file
@ -0,0 +1,18 @@
|
||||
//
|
||||
// CKCompressedImage.h
|
||||
// ReactNativeCameraKit
|
||||
//
|
||||
// Created by Sergey Ilyevsky on 15/05/2017.
|
||||
// Copyright © 2017 Wix. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface CKCompressedImage : NSObject
|
||||
|
||||
- (instancetype)initWithImage:(UIImage *)image imageQuality:(NSString*)imageQuality;
|
||||
|
||||
@property (nonatomic, readonly) UIImage *image;
|
||||
@property (nonatomic, readonly) NSData *data;
|
||||
|
||||
@end
|
||||
48
ios/lib/ReactNativeCameraKit/CKCompressedImage.m
Normal file
48
ios/lib/ReactNativeCameraKit/CKCompressedImage.m
Normal file
@ -0,0 +1,48 @@
|
||||
//
|
||||
// CKCompressedImage.m
|
||||
// ReactNativeCameraKit
|
||||
//
|
||||
// Created by Sergey Ilyevsky on 15/05/2017.
|
||||
// Copyright © 2017 Wix. All rights reserved.
|
||||
//
|
||||
|
||||
#import "CKCompressedImage.h"
|
||||
|
||||
@implementation CKCompressedImage
|
||||
|
||||
- (instancetype)initWithImage:(UIImage *)image imageQuality:(NSString*)imageQuality
|
||||
{
|
||||
if(self = [super init]) {
|
||||
CGFloat max = 1200.0f;
|
||||
if ([imageQuality isEqualToString:@"high"]) {
|
||||
max = 1200.0f;
|
||||
}
|
||||
else if ([imageQuality isEqualToString:@"medium"]) {
|
||||
max = 800.0f;
|
||||
}
|
||||
else {
|
||||
_image = image;
|
||||
_data = UIImageJPEGRepresentation(image, 1.0f);
|
||||
}
|
||||
float actualHeight = image.size.height;
|
||||
float actualWidth = image.size.width;
|
||||
|
||||
float imgRatio = actualWidth/actualHeight;
|
||||
|
||||
float newHeight = (actualHeight > actualWidth) ? max : max/imgRatio;
|
||||
float newWidth = (actualHeight > actualWidth) ? max*imgRatio : max;
|
||||
|
||||
|
||||
CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
|
||||
UIGraphicsBeginImageContext(rect.size);
|
||||
[image drawInRect:rect];
|
||||
_image = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
_data = UIImageJPEGRepresentation(_image, 0.85f);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@ -14,6 +14,7 @@
|
||||
|
||||
#import "CKGalleryManager.h"
|
||||
#import "CKGalleryViewManager.h"
|
||||
#import "CKCompressedIMage.h"
|
||||
|
||||
typedef void (^AlbumsBlock)(NSDictionary *albums);
|
||||
|
||||
@ -223,14 +224,16 @@ RCT_EXPORT_METHOD(resizeImage:(NSDictionary*)image
|
||||
NSData *data = [NSData dataWithContentsOfURL:url];
|
||||
|
||||
UIImage *originalImage = [UIImage imageWithData:data];
|
||||
NSData *compressedImageData = [CKGalleryManager compressImage:originalImage imageQuality:quality];
|
||||
CKCompressedImage *compressedImage = [[CKCompressedImage alloc] initWithImage:originalImage imageQuality:quality];
|
||||
|
||||
|
||||
NSURL *temporaryFileURL = [CKCamera saveToTmpFolder:compressedImageData];
|
||||
NSURL *temporaryFileURL = [CKCamera saveToTmpFolder:compressedImage.data];
|
||||
if (temporaryFileURL) {
|
||||
ans[@"uri"] = temporaryFileURL.description;
|
||||
ans[@"name"] = temporaryFileURL.lastPathComponent;
|
||||
ans[@"size"] = @(data.length);
|
||||
ans[@"size"] = @(compressedImage.data.length);
|
||||
ans[@"width"] = @(compressedImage.image.size.width);
|
||||
ans[@"height"] = @(compressedImage.image.size.height);
|
||||
}
|
||||
}
|
||||
resolve(ans);
|
||||
@ -413,33 +416,4 @@ RCT_EXPORT_METHOD(deleteTempImage:(NSString*)tempImageURL
|
||||
}
|
||||
|
||||
|
||||
+(NSData *)compressImage:(UIImage *)image imageQuality:(NSString*)imageQuality{
|
||||
CGFloat max = 1200.0f;
|
||||
if ([imageQuality isEqualToString:@"high"]) {
|
||||
max = 1200.0f;
|
||||
}
|
||||
else if ([imageQuality isEqualToString:@"medium"]) {
|
||||
max = 800.0f;
|
||||
}
|
||||
else {
|
||||
return UIImageJPEGRepresentation(image, 1.0f);
|
||||
}
|
||||
float actualHeight = image.size.height;
|
||||
float actualWidth = image.size.width;
|
||||
|
||||
float imgRatio = actualWidth/actualHeight;
|
||||
|
||||
float newHeight = (actualHeight > actualWidth) ? max : max/imgRatio;
|
||||
float newWidth = (actualHeight > actualWidth) ? max*imgRatio : max;
|
||||
|
||||
|
||||
CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
|
||||
UIGraphicsBeginImageContext(rect.size);
|
||||
[image drawInRect:rect];
|
||||
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
|
||||
NSData *imageData = UIImageJPEGRepresentation(img, 0.85f);
|
||||
UIGraphicsEndImageContext();
|
||||
return imageData;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@ -657,33 +657,4 @@ RCT_EXPORT_METHOD(modifyGalleryViewContentOffset:(NSDictionary*)params) {
|
||||
}
|
||||
|
||||
|
||||
+(UIImage *)compressImage:(UIImage *)image imageQuality:(NSString*)imageQuality{
|
||||
CGFloat max = 1200.0f;
|
||||
if ([imageQuality isEqualToString:@"high"]) {
|
||||
max = 1200.0f;
|
||||
}
|
||||
else if ([imageQuality isEqualToString:@"medium"]) {
|
||||
max = 800.0f;
|
||||
}
|
||||
else {
|
||||
return image;
|
||||
}
|
||||
float actualHeight = image.size.height;
|
||||
float actualWidth = image.size.width;
|
||||
|
||||
float imgRatio = actualWidth/actualHeight;
|
||||
|
||||
float newHeight = (actualHeight > actualWidth) ? max : max*(actualWidth/actualHeight);
|
||||
float newWidth = (actualHeight > actualWidth) ? max*(actualWidth/actualHeight) : max;
|
||||
|
||||
|
||||
CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
|
||||
UIGraphicsBeginImageContext(rect.size);
|
||||
[image drawInRect:rect];
|
||||
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
return img;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user