Solved

xcode how to save an image

Posted on 2010-09-20
4
2,298 Views
Last Modified: 2012-05-10
how do you save an image after you take a picture? In my app i'm using an UIImagePickerController that takes the picture but i don't know how to save the image.
0
Comment
Question by:BTMExpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33722320
If you need to save it in the album: UIImageWriteToSavedPhotosAlbum
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html

Another way:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/40279-save-uiimagepickercontroller-user-default.html

The simplest case: in the didFinish you can save the image:
UIImageWriteToSavedPhotosAlbum(image, nil,nil,nil);

But this way will look better:
UIImageWriteToSavedPhotosAlbum(reconstructedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
and [image autorelease]; in the callback.
As it's shown here:
http://stackoverflow.com/questions/3017681/uiimagewritetosavedphotosalbum-working-sometimes

Another way.
The first step is always to convert UIImage to NSData:
NSData *ImageData = UIImageJPEGRepresentation(img, 0.9);

Now, when it's already NSData, we can save it. In that thread people saved in the preferences:
[[NSUserDefaults standardUserDefaults] setObject:ImageData forKey:@"image"];
      
More info about the question:
http://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more

0
 

Author Comment

by:BTMExpert
ID: 33724869
that helped so much thank you.  One small problem now.  everytime i click the button to save my image, it saves which is great but when i try to do anything else after that, the app crashes.

- (IBAction)saveImage {
      UIImage *image = imageView.image;      

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Image" message:@"Image" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
      [alert show];
      [alert release];
      
      UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

- (void)image:(UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
      NSLog(@"SAVE IMAGE COMPLETE");
      if(error != nil) {
            NSLog(@"ERROR SAVING:%@",[error localizedDescription]);
      }
      [image autorelease];
}
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33724898
Maybe, because of that line:  [image autorelease];

try to cmment it. If it does not help, comment the alert (all lthree lines about the alert).
 
0
 

Author Comment

by:BTMExpert
ID: 33724911
i took the [image release]; out and it works now. Thanks a lot!!!!!!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question