Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:04:21+00:00 2026-05-27T21:04:21+00:00

So In my app, I want the user to be able to select/take a

  • 0

So In my app, I want the user to be able to select/take a picture, then show that image in a UIImageView. The problem is, through my research, I’ve found many ways to save/load images, but apparently there is a problem with knowing whether the image is a .png or .jpg. So I’ve tried a bunch of different thing, and can’t seem to get it to work. Thanks in advance for any help.

Heres my code for once the image is selected (I give the user the option to take a picture or select a picture from the camera roll):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (!image) image = [info objectForKey:UIImagePickerControllerOriginalImage];

    if (!image)
       return;

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *whichPic = @""
    if (whichPicture == pictureA)
        whichPic = @"kpictureA";
    else
        whichPic = @"kpictureB";

    [defaults setObject:imageData forKey:whichPic];
    [defaults synchronize];

I Tried this, but couldn’t get it to work:

// Create paths to output images
    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.png"];
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.jpg"];

    // Write a UIImage to JPEG with minimum compression (best quality)
    // The value 'image' must be a UIImage object
    // The value '1.0' represents image compression quality as value from 0.0 to 1.0
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];

    // Write image to PNG
    [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

    // Let's check to see if files were successfully written...

    // Create file manager
    NSError *error;
    NSFileManager *fileMgr = [NSFileManager defaultManager];

    // Point to Document directory
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    // Write out the contents of home directory to console
    NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
    */

I also tried this, but to no avail:

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
    [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:metadata completionBlock:nil];

Then to load the image when the view loads, I tried this:

    // Create paths to output images
NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.png"];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Before.jpg"];


// Let's check to see if files were successfully written...

// Create file manager
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];

// Point to Document directory
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

// Write out the contents of home directory to console
NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);


if ([[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error] containsObject:@"pictureA.png"]) {
    NSData *imageData = [fileMgr contentsAtPath:@"Documents/Before.png"];
    [pcitureAButton setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
}
else if ([fileMgr contentsAtPath:@"Documents/pictureA.png"]) {
    NSData *imageData = [fileMgr contentsAtPath:@"Documents/pictureA.png"];

    [pictureButton setImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal]; 
}

Then I tried this:

  if([defaults valueForKey:@"kPictureA"]) {
    UIImage *image = [[UIImage alloc] initWithData:[defaults valueForKey:kPictureA]];
   [pictureAButton setImage:image forState:UIControlStateNormal];
}

and finally I tried a using NSData with NSUserDefeaults.

I would prefer to use NSUserDefaults because I’m most comfortable with them, but I certainly open to solution that works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:04:21+00:00Added an answer on May 27, 2026 at 9:04 pm

    This is what I use:

    Take a photo

    - (IBAction)openCamera:(id)sender{
    
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES){
    
            UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
            cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
            cameraUI.allowsEditing = YES;
            cameraUI.delegate = self;
            //Displays only picture option;        
            cameraUI.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
    
            [self presentModalViewController: cameraUI animated: YES];
    
            [cameraUI release];
    
        }else{
            [self openPhotoLibrary:nil];
        }
    }
    

    Choose photo from library

    - (IBAction)openPhotoLibrary:(id)sender{
    
        UIImagePickerController *libraryPicker = [[UIImagePickerController alloc] init];
        libraryPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        libraryPicker.allowsEditing = YES;
        libraryPicker.delegate = self;
        //Displays only picture option;
        libraryPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
    
        [self presentModalViewController: libraryPicker animated: YES];
    
        [libraryPicker release];
    }
    

    Responding to the user tapping Cancel.

    - (void)imagePickerControllerDidCancel: (UIImagePickerController *) picker {
    
        [[picker parentViewController] dismissModalViewControllerAnimated: YES];
        [picker release];
    }
    

    Responding to the user accepting a newly-captured picture

    - (void) imagePickerController: (UIImagePickerController *) picker
     didFinishPickingMediaWithInfo: (NSDictionary *) description {
    
        NSString *mediaType = [description objectForKey: UIImagePickerControllerMediaType];
        UIImage *originalImage, *editedImage, *imageToSave;
    
        // Handle a still image capture
        if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
    
            editedImage = (UIImage *) [description objectForKey:UIImagePickerControllerEditedImage];
            originalImage = (UIImage *) [description objectForKey:UIImagePickerControllerOriginalImage];
    
            if (editedImage) {
                imageToSave = editedImage;
            } else {
                imageToSave = originalImage;
            }
    
            // Save the new image (original or edited) to the Camera Roll
            if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
                UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
    
    
            //Save your UIImage here
    
            myImage = imageToSave;
    
        }
    
        [self dismissModalViewControllerAnimated:YES];
    }
    

    EDIT

    To check the file type you can examin the first NSData byte using the following method:

    + (NSString *)contentTypeForImageData:(NSData *)data {
        uint8_t c;
        [data getBytes:&c length:1];
    
        switch (c) {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
        }
        return nil;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want authenticate user in my android app through google login and then dispaly
I'm working on a painting app, and I want to user to be able
I want to make the user of the app I'm developing, able to load
After the first launch of my app i want to show the user a
I have a dialog in an Android app that I don't want the user
I want to take a user OUT of my app and into the App
I want the user to be able to record audio in an app, and
Basically, the app displays images, and I want the user to be able to
For a new SaaS app that I'm working on, I want the end user
I have a picture app with a queue in it. I want the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.