add upload from camera and gallery view after images screen

This commit is contained in:
Ran Greenberg 2016-07-02 01:11:56 +03:00
parent c6dbf4356e
commit fef991a8ce
7 changed files with 132 additions and 77 deletions

View File

@ -107,7 +107,6 @@ RCT_ENUM_CONVERTER(CKCameraZoomMode, (@{
- (void)removeReactSubview:(UIView *)subview
{
[subview removeFromSuperview];
return;
}
@ -436,21 +435,26 @@ RCT_ENUM_CONVERTER(CKCameraZoomMode, (@{
imageInfoDict[@"size"] = [NSNumber numberWithInteger:imageData.length];
if (shouldSaveToCameraRoll) {
[self saveImageToCameraRoll:imageData temporaryFileURL:temporaryFileURL];
}
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:self.fetchOptions];
PHAsset *lastImageAsset = [fetchResult firstObject];
if (lastImageAsset.localIdentifier) {
imageInfoDict[@"id"] = lastImageAsset.localIdentifier;
}
if (block) {
block(imageInfoDict);
[self saveImageToCameraRoll:imageData temporaryFileURL:temporaryFileURL block:^(BOOL success) {
if (success) {
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:self.fetchOptions];
PHAsset *lastImageAsset = [fetchResult lastObject];
if (lastImageAsset.localIdentifier) {
imageInfoDict[@"id"] = lastImageAsset.localIdentifier;
}
if (block) {
block(imageInfoDict);
}
}
else {
NSLog( @"Could not save to camera roll");
}
}];
}
}
}];
@ -522,7 +526,10 @@ RCT_ENUM_CONVERTER(CKCameraZoomMode, (@{
-(void)saveImageToCameraRoll:(NSData*)imageData temporaryFileURL:(NSURL*)temporaryFileURL{
-(void)saveImageToCameraRoll:(NSData*)imageData
temporaryFileURL:(NSURL*)temporaryFileURL
block:(CallbackBlock)block{
// To preserve the metadata, we create an asset from the JPEG NSData representation.
// Note that creating an asset from a UIImage discards the metadata.
// In iOS 9, we can use -[PHAssetCreationRequest addResourceWithType:data:options].
@ -533,6 +540,10 @@ RCT_ENUM_CONVERTER(CKCameraZoomMode, (@{
} completionHandler:^( BOOL success, NSError *error ) {
if ( ! success ) {
NSLog( @"Error occurred while saving image to photo library: %@", error );
block(NO);
}
else {
block(YES);
}
}];
}
@ -548,6 +559,10 @@ RCT_ENUM_CONVERTER(CKCameraZoomMode, (@{
} completionHandler:^( BOOL success, NSError *error ) {
if ( ! success ) {
NSLog( @"Error occurred while saving image to photo library: %@", error );
block(NO);
}
else {
block(YES);
}
}];
}
@ -746,6 +761,7 @@ RCT_ENUM_CONVERTER(CKCameraZoomMode, (@{
- (void)removeObservers
{
NSLog(@"############################ removeObservers");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.session removeObserver:self forKeyPath:@"running" context:SessionRunningContext];

View File

@ -9,6 +9,7 @@
#import "CKGalleryManager.h"
#import <Photos/Photos.h>
#import "RCTConvert.h"
#import "CKGalleryViewManager.h"
typedef void (^AlbumsBlock)(NSDictionary *albums);
@ -159,6 +160,36 @@ RCT_EXPORT_METHOD(getAlbumsWithThumbnails:(RCTPromiseResolveBlock)resolve
}];
}
RCT_EXPORT_METHOD(getImagesForIds:(NSArray*)imagesIdArray
resolve:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject) {
NSMutableArray *assetsArray = [[NSMutableArray alloc] init];
PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:imagesIdArray options:nil];
for (PHAsset *asset in assets) {
NSDictionary *assetInfoDict = [CKGalleryViewManager infoForAsset:asset imageRequestOptions:imageRequestOptions];
[assetsArray addObject:@{@"uri": assetInfoDict[@"uri"],
@"size": assetInfoDict[@"size"],
@"name": assetInfoDict[@"name"]}];
}
if (resolve) {
resolve(@{@"images": assetsArray});
}
}
@end

View File

@ -17,5 +17,7 @@
@interface CKGalleryViewManager : RCTViewManager
+(NSMutableDictionary*)infoForAsset:(PHAsset*)asset imageRequestOptions:(PHImageRequestOptions*)imageRequestOptions;
@end

View File

@ -51,11 +51,11 @@ static NSString * const CellReuseIdentifier = @"Cell";
self.selectedAssets = [[NSMutableArray alloc] init];
self.imageManager = [[PHCachingImageManager alloc] init];
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchOptions *albumsOptions = [[PHFetchOptions alloc] init];
albumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
// PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
// allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
//
// PHFetchOptions *albumsOptions = [[PHFetchOptions alloc] init];
// albumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
self.imageRequestOptions = [[PHImageRequestOptions alloc] init];
self.imageRequestOptions.synchronous = YES;
@ -118,7 +118,7 @@ static NSString * const CellReuseIdentifier = @"Cell";
-(void)setAlbumName:(NSString *)albumName {
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
if ([albumName caseInsensitiveCompare:@"all photos"] == NSOrderedSame || !albumName || [albumName isEqualToString:@""]) {
// self.assetsCollection = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
@ -242,52 +242,6 @@ static NSString * const CellReuseIdentifier = @"Cell";
}
+(NSMutableDictionary*)infoForAsset:(PHAsset*)asset imageRequestOptions:(PHImageRequestOptions*)imageRequestOptions {
NSError *error = nil;
NSURL *directoryURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]] isDirectory:YES];
__block NSMutableDictionary *assetInfoDict = nil;
[[PHCachingImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
NSURL *fileURLKey = info[@"PHImageFileURLKey"];
if (fileURLKey) {
assetInfoDict = [[NSMutableDictionary alloc] init];
NSString *fileName = ((NSURL*)info[@"PHImageFileURLKey"]).lastPathComponent;
if (fileName) {
assetInfoDict[@"name"] = fileName;
}
float imageSize = imageData.length;
assetInfoDict[@"size"] = [NSNumber numberWithFloat:imageSize];
NSURL *fileURL = [directoryURL URLByAppendingPathComponent:fileName];
NSError *error = nil;
[imageData writeToURL:fileURL options:NSDataWritingAtomic error:&error];
if (!error && fileURL) {
assetInfoDict[@"uri"] = fileURL.absoluteString;
}
else if (error){
NSLog(@"%@", error);
}
}
}];
if (assetInfoDict && asset) {
assetInfoDict[@"asset"] = asset;
}
return assetInfoDict;
}
@end
@interface CKGalleryViewManager ()
@ -368,5 +322,57 @@ RCT_EXPORT_METHOD(getSelectedImages:(RCTPromiseResolveBlock)resolve
}
}
+(NSMutableDictionary*)infoForAsset:(PHAsset*)asset imageRequestOptions:(PHImageRequestOptions*)imageRequestOptions {
NSError *error = nil;
NSURL *directoryURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]] isDirectory:YES];
[[NSFileManager defaultManager] createDirectoryAtURL:directoryURL withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"ERROR while creating directory:%@",error);
}
__block NSMutableDictionary *assetInfoDict = nil;
[[PHCachingImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
NSURL *fileURLKey = info[@"PHImageFileURLKey"];
if (fileURLKey) {
assetInfoDict = [[NSMutableDictionary alloc] init];
NSString *fileName = ((NSURL*)info[@"PHImageFileURLKey"]).lastPathComponent;
if (fileName) {
assetInfoDict[@"name"] = fileName;
}
float imageSize = imageData.length;
assetInfoDict[@"size"] = [NSNumber numberWithFloat:imageSize];
NSURL *fileURL = [directoryURL URLByAppendingPathComponent:fileName];
NSError *error = nil;
[imageData writeToURL:fileURL options:NSDataWritingAtomic error:&error];
if (!error && fileURL) {
assetInfoDict[@"uri"] = fileURL.absoluteString;
}
else if (error){
NSLog(@"%@", error);
}
}
}];
if (assetInfoDict && asset) {
assetInfoDict[@"asset"] = asset;
}
return assetInfoDict;
}
@end

View File

@ -20,20 +20,16 @@ export default class CameraKitCamera extends React.Component {
async capture(saveToCameraRoll = true) {
const imageTmpPath = await NativeCameraAction.capture(saveToCameraRoll);
console.log(imageTmpPath);
return imageTmpPath;
}
async changeCamera() {
const success = await NativeCameraAction.changeCamera();
console.log(success);
return success;
}
async setFleshMode(flashMode = 'auto') {
console.log(flashMode);
const success = await NativeCameraAction.setFlashMode(flashMode);
console.log(success);
return success;
}
}

View File

@ -1,16 +1,21 @@
import _ from 'lodash';
import {
NativeModules,
CameraRoll
} from 'react-native';
var CKGallery = NativeModules.CKGalleryManager;
import _ from 'lodash';
async function getAlbumsWithThumbnails() {
const albums = await CKGallery.getAlbumsWithThumbnails();
return albums;
}
async function getImagesForIds(imagesId = []) {
const images = await CKGallery.getImagesForIds(imagesId);
return images;
}
//function getPhotosForAlbum(albumName, numberOfPhotos, callback, error) {
//
@ -33,4 +38,5 @@ async function getAlbumsWithThumbnails() {
export default {
getAlbumsWithThumbnails,
getImagesForIds
}

View File

@ -25,6 +25,4 @@ export default class CameraKitGalleryView extends Component {
const selectedImages = await GalleryViewManager.getSelectedImages();
return selectedImages;
}
}