Image from UIImagePicker to NSMutableArray - ios

Very simply, I want to take a photo using UIImagePicker and place it into an NSMutableArray. I hve the following code below. The camera part works fine and for test purposes the image is displayed in a UIImage called photoImageView. However I can't seem to add the image into the array. Any help gratefully received.
#property (nonatomic, retain) NSMutableArray *photos;
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *photoTaken = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
photoImageView.image=photoTaken;
[photos addObject:[UIImage photoTaken];
[picker dismissViewControllerAnimated:YES completion:nil];
}

1)Are you initialising the photos NSMutableArray? Sometimes addObject: won't throw an error when trying to insert an object into a NULL NSArray.
2)
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *photoTaken = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];
if (photoTaken)
{
photoImageView.image=photoTaken;
[photos addObject:photoTaken];
}else{
NSLog(#"Selected photo is NULL");
}
[picker dismissViewControllerAnimated:YES completion:nil];
}

Have you instantiated the array somewhere (viewDidLoad for example):
self.photos = [NSMutableArray alloc] init];
?

Modify below after adding to (Ian L )
[photos addObject: photoTaken];

photoTaken is already instantiated as an UIImage object. No need to do :
[photos addObject : [UIImage photoTaken]];
Simply do this :
[photos addObject : photoTaken];

Related

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 :)

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

How do I allow user to use their saved photo as a background in my app?

I have been searching around for how to do this sporadically for the last few days and havent been able to find anything and hope you can help.
Currently in my app I have 5 diffrent background as part of the project fils and allow the user to chose between them to set the app background to give some customization aspict to the app.
What I am looking to do is allow the user to select a photo that they have saved on thier device and use that as a background for my app but am unable to find out how to do it or perhaps missed it without knowing (I come from a primarraly C# background so still adjusting to termanology and all the fun things that come with learning a know lang and IDE).
What Im looking to do in code is:
User clicks to use thier own image
app opens window to browse through images
user selects image to use
assign and save path (or save image in the app somehow)
set imageview to that image
Thank you everyone for any assistance you can give and the time it takes you to give it. As a FYI Im building for iPhone 5.0
Make a UIImageView in nib file and button and hook the nib
In ViewController.h
#interface ViewController : UIViewController
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
IBOutlet UIButton *ChooseFileBtn;
}
#property (nonatomic,retain) UIImage *userimage;
-(IBAction)getphoto:(id)sender;
In ViewController.m
-(IBAction)getphoto:(id)sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
userimage = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image=userimage;
}
You can use UIImagePickerController to get image from Photo library
-(void)ClickOnsetBG:(id)sender{
UIImagePickerController *imgpicker = [[UIImagePickerController alloc] init];
imgpicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
imgpicker.delegate=self;
[self.navigationController presentModalViewController:imgpicker animated:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView commitAnimations];
}
Then implement delegate method to get selected image:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *aImgRecord = [info objectForKey:UIImagePickerControllerOriginalImage];
[imgpicker dismissModalViewControllerAnimated:YES];
bgImage.image =aImgRecord;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"custombg.png"];
NSData *imageData = UIImagePNGRepresentation(aImgRecord);
[imageData writeToFile:savedImagePath atomically:NO];
}
This image will be saved in document directory of you app. So when you will reopen your app, you will have to set bgimage from This document directory path.
You can do this with NSUserDefaults also
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage *newImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSData *imageData = UIImageJPEGRepresentation(newImage, 1);
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:#"BackgroundImage"];
[picker dismissViewControllerAnimated:YES completion:^ {
// If you have used UIImageView
[self.yourImageViewObject setImage:newImage];
}];
}
Later the image could be easily shown through NSUserDefaults like
UIImage *image = [UIImage imageWithData:[[NSUserDefaults standardUserDefaults] objectForKey:#"BackgroundImage"]];

ios pushViewController from imagePickerController: didFinishPickingMediaWithInfo

in my app after i take a photo with the camera i want to navigate to an other viewcontroller in
didFinishPickingMediaWithInfo
Right now, i'm doing it in the dismissViewControllerAnimated function's completition block, but i don't feel like it's the most elegant way to do it. Could you tell me how this could be done better? Here's the exact code
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info[UIImagePickerControllerOriginalImage] scaledCopyOfSize:CGSizeMake(175.0f, 175.0f)];
[self dismissViewControllerAnimated:YES completion:^(void){
setVC = [[SetVCr alloc]init];
[[self navigationController]pushViewController:seVC animated:YES];
}];
}
}
You can push new view controller directly from the picker
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info[UIImagePickerControllerOriginalImage] scaledCopyOfSize:CGSizeMake(175.0f, 175.0f)];
setVC = [[SetVCr alloc]init];
[picker pushViewController:seVC animated:YES];//you can push new view controller directly from the picker
}
}
I suggest setting your completion block to nil and on the next line call the 'performSegueWithIdentifier' method as written below:
[self performSegueWithIdentifier:#"segueToNewcontroller" sender:self];

UIImagepickercontroller Not Selecting Image

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..

Resources