Saving a Image when exit

Discussion in 'iOS Development' started by EmpoweredDesignApps, May 31, 2010.

  1. EmpoweredDesignApps

    EmpoweredDesignApps New Member

    Joined:
    May 31, 2010
    Messages:
    2
    Likes Received:
    0
    Device:
    2G iPod touch
    Hey guys!

    I have make a CLock app that I have implement the ability to change the background image. I have a UIImagePickerView to select a Image from my library, and set it up as the wallpaper. All works, the image picker, the timers, everything. But when I exit, the changes are gone. It will go back before. (No changes where saved). How can I save that image.

    this is my code:

    in my .h
    [OBJC]
    UIImageView * theimageView;
    }
    @property (nonatomic, retain) IBOutlet UIImageView * theimageView;
    [/OBJC]

    my .m:

    [OBJC]-(IBAction)buttonForPiture {



    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle

    Please Register or Log in to view images

    "Cancel" destructiveButtonTitle:nil otherButtonTitles

    Please Register or Log in to view images

    "Take Picture", @"Choose Picture", nil];
    [actionSheet showInView:self.view];
    [actionSheet release];

    }

    -(void)actionSheet

    Please Register or Log in to view images

    UIActionSheet *)actionSheet clickedButtonAtIndex

    Please Register or Log in to view images

    NSInteger)buttonIndex {

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if (actionSheet == actionSheet) {
    if (buttonIndex == 0) {





    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    else {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle

    Please Register or Log in to view images

    "Camera not supported" message

    Please Register or Log in to view images

    "Camera is not supported on this device." delegate:self cancelButtonTitle

    Please Register or Log in to view images

    "Close" otherButtonTitles:nil];
    [alert show];
    [alert release];
    }



    }
    if (actionSheet == actionSheet) {
    if (buttonIndex == 1) {

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentModalViewController

    Please Register or Log in to view images

    icker animated:YES];


    }
    }
    }
    }[/OBJC]



    Can someone tech me hoe to save it?

    Thanks!
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    use UIImageJPEGRepresentation() to get an NSData object holding the image data, then you can either save it with -[NSUserDeafaults setObject:forKey], or write it to a file in your docs directory. Upon application launch, or whenever you need to use the saved image, fetch it from NSUserDefaults or read it from the file.
  3. EmpoweredDesignApps

    EmpoweredDesignApps New Member

    Joined:
    May 31, 2010
    Messages:
    2
    Likes Received:
    0
    Device:
    2G iPod touch
    can you show me an example?
  4. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I don't think that should be necessary; both solutions I gave you are well-documented on developer.apple.com or eleswhere on the web.

    NSUserDefaults would be the simplest way to do it, though if you need more flexibility, you can write and read your own file(s).

Share This Page