Assigning image from photo album to a variable Objective-c - ios

I am getting image from a photo album with this code:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:nil];}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];}
Please Could anyone help me to assign this image to a variable? I want to blur it later using stackblur:
UIImage *newIma=[oldIma stackBlur:radius];
Thanks in advance!

Set the UIImage when you're getting the image that the user has selected.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
// Create the UIImage in your #implementation and set it here
myImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
myImageView.image = myImage;
}

Related

getting null when printing an image in didFinishPickingMediaWithInfo method

I am trying to pick an image from photo library and for that I used picker controller, but when I am trying to print the image in didFinishPickingMediaWithInfo method I am getting a null response. I used protocols also in my .h file.
- (IBAction)choosePhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSLog(#"choose the img");
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.imageView.image = info[UIImagePickerControllerEditedImage];
NSLog(#"imgae is %#",self.imageView.image);
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
// here you can check that image is null or not
[Btn_ImagePicker setImage:chosenImage forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:YES completion:NULL];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
For the full detail kindly print NSLog(#"%#",info); info contains full details.

ImagePickerController for iOS

I am trying to select a picture from the device's photo library.
This is my code so far. I am following a tutorial and that should work, but I am getting an error :
"Use of undeclared identifier info", on the second method, line 3.
What am I doing wrong?
- (IBAction)selectPicturePressed:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
UIImage *chosenImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
self.imgToUpload.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Change info to editInfo will resolve it.

How to load a UIImage from PhotoStream with UIImagePicker and Photo Framework under iOS 8.1?

How do I use the new Photo Framework to get a UIImage from a given image URL which the UIImagePicker returns?
I hope I understood you right...
You can open an UIImagePickerController like this:
- (IBAction)selectPhoto:(UIButton *)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
And after picking you get the UIImage from the pickers delegate like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
The chosenImage will be your Image
EDITED after complain
Im sorry dude... the Key I have chosen is only available if you set picker.allowsEditing = true; property on true...
So if you don't need the editing after you have chosen your image, use this one
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
instead of
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
I have just tried it, it will work...

Upload image from UIImagePicker to server via php

I have an UIImageView which load profile picture of the user. I add some UIImagePicker features and now the user able to change the profile picture either by using camera or add from library. This is the code:
- (IBAction)cameraAction:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)libraryAction:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
#pragma mark - Image Picker Controller delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
_profilePhoto.image = chosenImage;
UIImageWriteToSavedPhotosAlbum(_profilePhoto.image, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Now then, I had php files to upload the current image to server. The problem is, the input parameter for the php is image file name. I do looking around for quite some times and known that to get the image name, first I need to save it to apps document directory and rename it with the name I want later. So can anyone tell me how to save this image to apps document directory, how to get it and how to rename it with the one I want. Thanks in advance
Don't think that saving the file to document directory will generate you new name.
When I save something to documents I generate unique file name.
What I do is I pass to web server some generic name like "Upload.png".
Here is some pseudo code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
...
NSData *data = UIImagePNGRepresentation(choosenImage);
NSString *name = #"Uploaded.png"
// Upload the nsdata to server
[[PHPServer instance] uploadProfileImage:data named:name];
}

Taking a photo and saving it to the Camera Roll

I'm having a problem where I just want a simple solution to take a picture and then save it in my app. However so far all I can do is take the photo have it load in a UIImageView, however it does not save in the Camera Roll.
- (IBAction)takePhoto:(UIButton *)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Any help regarding this would be greatly appreciated. Believe me, I really need it.
You will want to do this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
UIImageWriteToSavedPhotosAlbum(chosenImage, nil, nil, nil);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Swift 4
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if(info[UIImagePickerControllerMediaType] as! String == "public.image"){
guard let curImage:UIImage = info[UIImagePickerControllerOriginalImage] as? UIImage else{
return
}
UIImageWriteToSavedPhotosAlbum(curImage, nil, nil, nil)
}
}

Resources