Here is the code for UIImagePickerController Delegate. In this Code when we select an image from UIImagePickerController then we call this delegate function which upload the selected image to your local application folder. You have to first find the path of folder from documents directory and then save the image to that specific folder.
There is one more thing that if you are uploading multiple images then the name of image should be dynamic otherwise next image will replace the previous image in that folder.
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* image;
if([[info valueForKey:@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"])
{
image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"DispayImages"];
//DislayImages is your folder name
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fileName = [stringPath stringByAppendingFormat:@"/imageName.jpg"];
personImageUrl=fileName;
[profileImageData writeToFile:fileName atomically:YES]; //upload file to folder path
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:fileName]);
profileImageData = UIImageJPEGRepresentation(image, 1.0);
NSString *imgBase64String= [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]; //convert image into base64 string
}
}
0 Comment(s)