Saving an image to the camera roll on the iPhone is as close as a method call in the UIKit. However, it takes a few steps to wrap together code to manage error handling and notification that the image has been saved. Let’s see how this works.
Let’s begin with the method description to save an image:
void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
completionTarget refers to the object in which the completionSelector can be found. In other words, what object has the method that will be called once the file write is complete? contextInfo is a void * that you can use to specify content to be passed to the completionSelector.
Obviously, the image is a required paramater. The other three are only needed if you prefer to be notified asynchronously when the write is complete. Here’s how a call to the above method might look:
// Image to save UIImage *img = [UIImage imageNamed:@"ImageName.png"]; // Request to save the image to camera roll UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
Here we are specifying that the selector to be called upon completion is within this object (self). A template for this selector could look as follows:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { // Was there an error? if (error != NULL) { // Show error message... } else // No errors { // Show message image successfully saved } }
Add code for displaying the appropriate message based on success or failure of the image save, and you’re good to go.
'iOS' 카테고리의 다른 글
UIView 변형 방법 (0) | 2011.06.24 |
---|---|
델리게이트 성격의 프로퍼티 설정 (0) | 2011.06.15 |
NSFileManager 디렉토리 위치 정의 (0) | 2011.06.11 |
objective C (xcode4) gcov 설정방법 (0) | 2011.06.07 |
단위 테스트시 plist가 제대로 읽어지지 않을때 (0) | 2011.05.31 |