User can select picture from camera or from gallery
///Select Picture from Gallery
-(IBAction)pickGalleryImage:(id)sender
{
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
imagePickerController.navigationBar.tintColor = [UIColor blackColor];
[self presentViewController:imagePickerController animated:YES completion:nil];
imagePickerMenu.hidden=true;
[imagePickerMenu removeFromSuperview];
[viewMain removeFromSuperview];
}
///Select Picture from Camera
-(IBAction)pickCameraImage:(id)sender
{
if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init] ;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//Permetto la modifica delle foto
picker.allowsEditing = YES;
//Imposto il delegato
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
//[self presentModalViewController:picker animated:YES];
imagePickerMenu.hidden=true;
[imagePickerMenu removeFromSuperview];
[viewMain removeFromSuperview];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CGSize imgSize =CGSizeMake(200, 200);
UIImage *image = [self imageWithImage:[info objectForKey:UIImagePickerControllerOriginalImage] scaledToSize:imgSize];
//Image Compression
NSData * imageData = UIImageJPEGRepresentation(image, 0.70);
NSLog(@"lenght %.2lu", (unsigned long)imageData.length);
NSDateFormatter* formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"MMddYYYY_HHmmss"];
NSString *lstrCurrentDatetime = [formatter stringFromDate:[NSDate date]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//creating image name
NSString *uId = @"1";
NSString *strImageName = [@"" stringByAppendingFormat:@"User_Profile_%@_%@.jpeg", uId,lstrCurrentDatetime ];
//saving to doc directory
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:strImageName];
[imageData writeToFile:savedImagePath atomically:NO];
[picker dismissViewControllerAnimated:YES completion:NULL];
[SVProgressHUD show];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
[self uploadGalleryImage:strImageName];
}
//Resize Image
-(UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( CGSizeMake(200, 200) );
[image drawInRect:CGRectMake(0,0,200,200)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark - Image Upload -
-(void)uploadGalleryImage:(NSString *)imgProfilePic
{
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [docpaths objectAtIndex:0];
NSString *serviceUrl = [NSString stringWithFormat:@"%@UploadProfilePic.php",kImageUpLoadURL];
// an url where the request to be posted
NSURL *url = [NSURL URLWithString:serviceUrl];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:url] ;
[request setURL:[NSURL URLWithString:serviceUrl]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:imgProfilePic];
NSMutableDictionary *dictUpload = [[NSMutableDictionary alloc] init];
NSMutableData *postbody = [NSMutableData data];
NSString *postData = [self getHTTPBodyParamsFromDictionary:dictUpload boundary:boundary];
[postbody appendData:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imagePath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
NSData *imageData = UIImageJPEGRepresentation(thumbNail, 0.60);
NSLog(@"lenght %lu", (unsigned long)[imageData length]);
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%i%@\"\r\n", 1, imgProfilePic] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
if(data == nil){
NSLog(@"Upload Failure");
[SVProgressHUD dismiss];
[self CallAlert:appName andMsg:@"Image could not be uploaded please try again or late"];
}
returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if([returnString containsString:@"Success"])
{
[SVProgressHUD dismiss];
profileImage = [returnString stringByReplacingOccurrencesOfString:@"Success." withString:@""];
NSString *newStr=[kProfileImagePath stringByAppendingFormat:@"%@",profileImage];
NSURL *imgUrl=[NSURL URLWithString:newStr];
[imgProfile sd_setImageWithURL:imgUrl];
}
// [SVProgressHUD dismiss];
}];
[dataTask resume];
}
-(NSString *) getHTTPBodyParamsFromDictionary: (NSDictionary *)params boundary:(NSString *)boundary
{
NSMutableString *tempVal = [[NSMutableString alloc] init];
for(NSString * key in params)
{
[tempVal appendFormat:@"\r\n--%@\r\n", boundary];
[tempVal appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n%@",key,[params objectForKey:key]];
}
return [tempVal description];
}