Skip to content

Instantly share code, notes, and snippets.

@himanshu-benzatine
Created December 27, 2016 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himanshu-benzatine/e4d8346dd220e2075858d516c273213e to your computer and use it in GitHub Desktop.
Save himanshu-benzatine/e4d8346dd220e2075858d516c273213e to your computer and use it in GitHub Desktop.
@interface RegisterSpecializeViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UINavigationControllerDelegate>
{
NSString *updateImgKey;
NSInteger imagePickerSelFor;
NSArray *keysOfProfileImg;
float xPoz;
NSString *tempFileName;
NSMutableDictionary *CertificatephotoData;
NSInteger pickerselectcount;
}
@property (nonatomic, retain) UIImagePickerController *imagePicker;
@property (strong, nonatomic) IBOutlet UICollectionView *CertificateCollectionview;
- (void)viewDidLoad {
[super viewDidLoad];
pickerselectcount = 0;
CertificatephotoData = [[NSMutableDictionary alloc]init];
[self.CertificateCollectionview reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (CertificatephotoData.count == 10) {
return 10;
}
else{
return CertificatephotoData.count + 1;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ComplaintCollectionCell" forIndexPath:indexPath];
if (cell == nil)
{
cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ComplaintCollectionCell" forIndexPath:indexPath];
}
UIImageView *thumbail = (UIImageView*) [cell viewWithTag:50];
UIButton *close = (UIButton*)[cell viewWithTag:51];
if(indexPath.row == 0)
{
//imgReview.image = nil;
[close setHidden:YES];
thumbail.image = [UIImage imageNamed:@"ic_upload_file.png"];
}else
{
//NSString *imgKey = [NSString stringWithFormat:@"reviewimg%u",indexPath.row+1];
[close setHidden:NO];
[close addTarget:self
action:@selector(CloseMethod:event:) forControlEvents:UIControlEventTouchDown];
keysOfProfileImg=[self sortImageKey];
id aKey = [keysOfProfileImg objectAtIndex:indexPath.row - 1];
id anObject = [CertificatephotoData objectForKey:aKey];
if ([anObject isKindOfClass:[NSData class]]) {
UIImage *image = [UIImage imageWithData:anObject];
[thumbail setImage:image];
}else
{
NSURL *imageURL = [NSURL URLWithString:anObject];
[thumbail sd_setImageWithURL:imageURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
}
}
thumbail.layer.cornerRadius = 8.0f;
thumbail.layer.masksToBounds = YES;
return cell;
}
-(NSArray*)sortImageKey{
NSArray *keys=[[NSArray alloc] init];
keysOfProfileImg=[[NSArray alloc] init];
keys=[CertificatephotoData allKeys];
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self"
ascending: NO];
return [keys sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize contentSize;
if (collectionView == self.CertificateCollectionview) {
contentSize = CGSizeMake(90.0f, 90.0f);
return contentSize;
}
return contentSize;
}
-(IBAction)CloseMethod:(UIButton*)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.CertificateCollectionview];
NSIndexPath *indexPath = [self.CertificateCollectionview indexPathForItemAtPoint: currentTouchPosition];
NSLog(@"%ld",(long)indexPath.row);
id aKey = [keysOfProfileImg objectAtIndex:indexPath.row - 1];
// keysOfProfileImg=[self sortImageKey];
// NSArray *keys = [keysOfProfileImg objectAtIndex:indexPath.row - 1];
//
// id aKey = [keys objectAtIndex:indexPath.row];
[CertificatephotoData removeObjectForKey:aKey];
[self.CertificateCollectionview reloadData];
[self collectionviewhight_constrain];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
imagePickerSelFor = kAddNewImage;
}else
{
imagePickerSelFor = kUpdateExistingImage;
keysOfProfileImg=[self sortImageKey];
id aKey = [keysOfProfileImg objectAtIndex:indexPath.row - 1];
updateImgKey = aKey;
}
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Choose Event Image From" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Gallery",@"Camera", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
}
#pragma -mark camera deletegates method
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
@try{
if(buttonIndex == 0){
[self cstmChooseImage:UIImagePickerControllerSourceTypePhotoLibrary];
}else if (buttonIndex == 1){
[self cstmChooseImage:UIImagePickerControllerSourceTypeCamera];
}
}
@catch (NSException *exception) {
NSLog(@"Exception:%@",exception);
}
}
//select the photo chooose option
-(void)cstmChooseImage: (UIImagePickerControllerSourceType)sourceOption{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceOption;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if(info[UIImagePickerControllerMediaType]== (NSString *)kUTTypeImage)
{
//image
// NSData *imageData = UIImageJPEGRepresentation([info valueForKey:UIImagePickerControllerOriginalImage], 0);
NSData *imageData = UIImageJPEGRepresentation([info valueForKey:UIImagePickerControllerOriginalImage], 0);
NSLog(@"Image Sized:%@",[NSByteCountFormatter stringFromByteCount:imageData.length countStyle:NSByteCountFormatterCountStyleMemory]);
pickerselectcount++;
NSString *imgKey = [NSString stringWithFormat:@"certificate%d",(int)pickerselectcount];
[picker dismissViewControllerAnimated:YES completion:^{
switch (imagePickerSelFor) {
case kAddNewImage:
[CertificatephotoData setValue:imageData forKey:imgKey];
break;
case kUpdateExistingImage:
[CertificatephotoData setValue:imageData forKey:updateImgKey];
break;
default:
break;
}
keysOfProfileImg=[self sortImageKey];
[self.CertificateCollectionview reloadData];
[self collectionviewhight_constrain];
}];
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void)cstmGetUploadImageName:(NSString *)filename{
tempFileName = filename;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment