UIImagepickercontroller Not Selecting Image - ios

I had this working in an old code that I didn’t end up using... now I’m trying it again and it isn’t working. I”m not sure what is wrong. I’d like the user to select an image and then have it available in imageView1 for display. I appreciate the assistance.
- (IBAction)pushPick
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate =self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
//[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage: (UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
imageView1.image = image;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}

Try
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
imageView1.image = image;
[picker release];
[self dismissModalViewControllerAnimated:YES];
}
And make sure the view controller responds to UIImagePickerControllerDelegate.
imagePickerController:didFinishPickingImage:editingInfo: was deprecated in iOS 3.0.

This is how I do this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
bgImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(128, 128), YES, 1.0);
[bgImage.image drawInRect:CGRectMake(0,0,128,128)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsFolder = [path objectAtIndex:0];
NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%#/specificImagedName.jpg", documentsFolder]];
if([imageData writeToFile:tmpPathToFile atomically:YES]){
//NSLog(#"write succesful");
}
///////////////////////////////////////////
[po dismissPopoverAnimated:YES];
[picker release];
[tmpPathToFile release];}
it generates a jpg file to the documents folder of your app. Hope it helps..

Related

uploaded photo in ui image viewer keep disappears

I can upload a photo into ui image viewer from camera roll but when I restart my app or go to another page and come back, the uploaded photo disappears. How do I keep them?
- (IBAction)selectImage:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{
UIImage *chosenImage = selectedImage;
self.homeImage.image = chosenImage;
[self.view setNeedsDisplay];
[picker dismissViewControllerAnimated:YES completion:NULL];
NSData *imageData = UIImagePNGRepresentation(chosenImage.images);
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"/myImage.png"];
[imageData writeToFile:imagePath atomically:YES];
}
I expect the second method to keep the image in the internal database and keep showing my uploaded image, but it doesn't.
You need to load the image from file and assign it to your view after view is created, something similar to:
- (void)viewDidLoad {
NSString * imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"/myImage.png"];
self.homeImage.image = [UIImage imageWithContentsOfFile:imagePath];
}

Use of undeclared Identifier 'image'

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:^
{
[self emailImage:image];
}
];
[picker release];
}
I am new to iOS and have just started developing apps. I know Swift language but I have to complete this in Objective C. I am not able to resolve this error.
The link to the images are as follows:
Use this Code,
- (void)imagePickerController:(UIImagePickerController *)photoPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType;
mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage ])
{
UIImage * image = (UIImage*) [info objectForKey:UIImagePickerControllerEditedImage];
imageAddView.image = [UIImage squareImageWithImage:image scaledToSize:CGSizeMake(640, 640)];
[photoPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
hope its helpful
your code should be like,
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image=info[UIImagePickerControllerEditedImage];
[self emailImage:image];
[self dismissViewControllerAnimated:YES completion:^
{
//perform anything after dismissing image picker controller
}
];
}
Hope this will help :)

Camera Multiple Image Capture in iOS

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([multipleCapture isOn])
{
images=info[UIImagePickerControllerOriginalImage];
currentDate=[NSDate date];
myString=[dateFormatter stringFromDate:currentDate];
customText=imagetext.text;
saveImage.dateTime=myString;
saveImage.imageName=info[UIImagePickerControllerOriginalImage];
saveImage.customText=customText;
[imageArray addObject:saveImage];
[imageArray sortUsingDescriptors:sortDescriptors];
if ([imageArray count] == 1)
{
UIGraphicsBeginImageContext(images.size);
myString=[[imageArray objectAtIndex:0]valueForKey:#"dateTime"];
[images drawInRect:CGRectMake(0, 0, images.size.width, images.size.height)];
NSDictionary* attributes = #{NSFontAttributeName : [UIFontboldSystemFontOfSize:75],NSStrokeColorAttributeName :[UIColor blackColor],NSForegroundColorAttributeName : [UIColor yellowColor],NSStrokeWidthAttributeName : #-2.0};
[myString drawAtPoint:CGPointMake(120,70)withAttributes:attributes];
[customText drawAtPoint:CGPointMake(870, 70)withAttributes:attributes];
newImage= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage,self,#selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
}
}
}
/* I worked on this code. The issue is when I try to make multiple shot it shows "Received Memory Warning" and my app got crashed.
In this I tried to count the images,at the same time It counts "Camera not ready mode" also So that my taken images saved multiple times.
How can I solve this issue ? */
Try this,
int counter;
NSMutableArray * imageArray;
-(void)takePicture
{
counter=0;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image= [info objectForKey:UIImagePickerControllerEditedImage];
[imageArray addObject:image];
counter++;
if (counter<PhotoCount)
{
[self dismissModalViewControllerAnimated:NO];
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self presentModalViewController:imagePicker animated:NO];
[imagePicker release];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
PhotoCount is the number of photos you want to take.

Local address of the photo or image taken with UIImagePickerController

I use the UIImagePickerController to take photo or to select it from an album. Here I put the selected image to the imageView :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.temporaryImageView.image = chosenImage;
[picker dismissViewControllerAnimated:YES completion:NULL];
}
But How can i get the address of the selected photo on the device? or the name of it? Is it possible?
May be this can help you;
NSMutableString *imageName = [[[NSMutableString alloc] initWithCapacity:0]];
CFUUIDRef ref = CFUUIDCreate(kCFAllocatorDefault);
if (ref) {
[imageName appendString:NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID))];
}
[imageName appendString:#".png"];
I also recommend you to check this answer;
https://stackoverflow.com/a/16184616/2622013

2 UIButtons to set 2 UIImageViews

I've 2 UIButtons, I want both buttons to pick an image, button1 is setting an image to imageview1 and button2 to imageView2. I now created button1 which picks an image and set imageView1 to that image, but if I'm creating button2, I don't now what I have to do in:
- (IBAction)chooseImage1:(id)sender {
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (IBAction)chooseImage2:(id)sender {
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:YES completion:nil];
}
UIImage *image;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *mediaURL;
mediaURL = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
image = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
imageView1.image=image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
Create an index in your .h file. Something like
NSUInteger *selectedImageIndex;
And in your .m file :
- (void)showImagePicker:(UIImagePickerControllerSourceType)source{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = source;
ipc.allowsEditing = YES;
ipc.delegate = self;
[self presentModalViewController:ipc animated:YES];
}
- (IBAction)chooseImage1:(id)sender {
selectedImageIndex = 1;
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (IBAction)chooseImage2:(id)sender {
selectedImageIndex = 2;
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:#"UIImagePickerControllerEditedImage"];
if (!img)
img = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
if (selectedImageIndex == 1)
imageView1.image=img;
else
imageView2.image = img;
[picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
But then again, this is just one way to do it.

Resources