#import <UIKit/UIKit.h>
@interface Viewcontroller:uiviewcontroller<uiimagepickercontrollerdelegate,uinavigationcontrollerdelegate, Uiactionsheetdelegate>
@property (Retain, nonatomic) Iboutlet Uiimageview *imageview;
@property (Retain, nonatomic) UIButton *savetofilebutton;
Open album
-(Ibaction) Openalbum: (ID) sender;
To read a picture from a folder
-(Ibaction) Readimage: (ID) sender;
@end
#import "ViewController.h"
Save to Folder button's tab, before selecting the picture, this button is hidden
#define SAVE_BUTTON_TAG 101
@interface Viewcontroller ()
@end
@implementation Viewcontroller
@synthesize ImageView = _imageview;
@synthesize Savetofilebutton = _savetofilebutton;
-(void) viewdidload
{
[Super Viewdidload];
Gets the button control according to the tag set
Self.savetofilebutton = (UIButton *) [Self.view Viewwithtag:save_button_tag];
Adding object events
[Self.savetofilebutton addtarget:self Action: @selector (savetofilebtntapped:) forControlEvents: UIControlEventTouchUpInside];
Set to not visible
Self.saveToFileButton.hidden = YES;
}
-(void) viewdidunload
{
[Self setimageview:nil];
[Self setsavetofilebutton:nil];
[Super Viewdidunload];
Release any retained subviews of the main view.
}
-(BOOL) Shouldautorotatetointerfaceorientation: (uiinterfaceorientation) interfaceorientation
{
Return (interfaceorientation! = Uiinterfaceorientationportraitupsidedown);
}
-(void) Dealloc {
[Self.imageview release];
[Self.savetofilebutton release];
[Super Dealloc];
}
-(Ibaction) Openalbum: (ID) Sender {
Uiactionsheet *actionsheet = [[Uiactionsheet alloc] initwithtitle:@ "Select picture" Delegate:self cancelbuttontitle:@ "Cancel" Destructivebuttontitle:nil otherbuttontitles:@ "Photo", @ "album", Nil];
[Actionsheet ShowInView:self.view];
[Actionsheet release];
}
To read a picture from a folder
-(Ibaction) Readimage: (ID) Sender {
NSString *imagepath = [self imagesavedpath:@ "image.png"];
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
Determine if a file exists
if (![ FileManager Fileexistsatpath:imagepath]) {
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Note" message:@ "file does not exist" Delegate:self cancelbuttontitle:@ "OK" otherbuttontitles:nil, nil];
[Alertview show];
[Alertview release];
}else {
To read a picture from a specified directory
UIImage *image = [UIImage Imagewithcontentsoffile:imagepath];
Self.imageView.image = image;
}
}
Save to File button event
-(void) savetofilebtntapped: (ID) Sender {
NSString *imagepath = [self imagesavedpath:@ "image.png"];
BOOL issavesuccess = [self saveToDocument:self.imageView.image withfilepath:imagepath];
if (issavesuccess) {
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Operation result" message:@ "Save picture Success" Delegate:self Cancelbuttontitle: @ "OK" otherbuttontitles:nil, nil];
[Alertview show];
[Alertview release];
}else {
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Operation result" message:@ "Save picture Failed" Delegate:self Cancelbuttontitle: @ "OK" otherbuttontitles:nil, nil];
[Alertview show];
[Alertview release];
}
}
Save the selected picture in the directory folder
-(BOOL) Savetodocument: (UIImage *) image withfilepath: (NSString *) FilePath
{
if (image = nil) | | (FilePath = nil) | | [FilePath isequaltostring:@ ""]) {
return NO;
}
@try {
NSData *imagedata = nil;
Get file name extension
NSString *extention = [FilePath pathextension];
if ([extention isequaltostring:@ "PNG"]) {
Returns picture data in PNG format
ImageData = uiimagepngrepresentation (image);
}else{
Returns the image data in JPG format, the second parameter is compression quality: 0:best 1:lost
ImageData = uiimagejpegrepresentation (image, 0);
}
if (ImageData = = Nil | | [imageData length] <= 0) {
return NO;
}
Writes a picture to a specified path
[ImageData Writetofile:filepath Atomically:yes];
return YES;
}
@catch (NSException *exception) {
NSLog (@ "Save picture failed");
}
return NO;
}
Save the picture to the ImageFile folder based on the picture name
-(NSString *) Imagesavedpath: (NSString *) imageName
{
Get Documents folder Directory
Nsarray *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentpath = [path objectatindex:0];
Get File Manager
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
Specify a new folder path
NSString *imagedocpath = [documentpath stringbyappendingpathcomponent:@ "ImageFile"];
Create a ImageFile folder
[FileManager Createdirectoryatpath:imagedocpath withintermediatedirectories:yes Attributes:nil Error:nil];
Returns the path to the saved picture (the picture is saved in the ImageFile folder)
NSString * ImagePath = [Imagedocpath stringbyappendingpathcomponent:imagename];
return imagePath;
}
#pragma Delegate method Uiimagepickercontrollerdelegate
The delegate method of the image picker, and then callback the method after selecting the picture
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) Image Editinginfo :(nsdictionary *) editinginfo
{
if (image = nil) {
Show photos on the screen after selecting them and make the Save button visible
Self.imageView.image = image;
Self.saveToFileButton.hidden = NO;
}
Close the image selector
[Self dismissmodalviewcontrolleranimated:yes];
}
-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex
{
Get Picture Picker
Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc] init];
Specify Proxy
Imagepicker.delegate = self;
Allow editing after opening a picture
imagepicker.allowsediting = YES;
Determine the type of picture source
if (Buttonindex = = 0) {
if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {
Camera
Imagepicker.sourcetype = Uiimagepickercontrollersourcetypecamera;
}
}else if (Buttonindex = = 1) {
if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) {
Photo Gallery
Imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
}
if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypesavedphotosalbum]) {
Album
Imagepicker.sourcetype = Uiimagepickercontrollersourcetypesavedphotosalbum;
// }
}else if (Buttonindex = = [Actionsheet Cancelbuttonindex]) {
Return
}
Open a picture Select modal view
[Self presentmodalviewcontroller:imagepicker animated:yes];
[Imagepicker release];
}
@end
Picture Save-use Nsfilemanager to create a specified directory save picture