获取本地路径的方法,在沙河路径中保存图片 ,将保存文件到沙河路径下 把图片转换成NSData类型的数据来保存文件

/*

 

 获取路径的方法

 

 */

#import "ViewController.h"

 

@interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property(nonatomic,copy)NSString * imagePath;

@property(nonatomic,strong)UIButton  * photoButton;

@property(nonatomic,strong)UIButton * camaraButton;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

//    获取documents文件夹目录

    NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * documentPath = [path objectAtIndex:0];

//    指定新建文件夹路径

    NSString * iageDocument = [documentPath stringByAppendingPathComponent:@"ImageFile"];

//    创建ImageFile文件夹

    [[NSFileManager defaultManager] createDirectoryAtPath:iageDocument withIntermediateDirectories:YES attributes:nil error:nil];

//保存图片的路径

    self.imagePath = [iageDocument stringByAppendingPathComponent:@"image.png"];

//    从相册或照相机中获取的图片内容

    self.photoButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 200)];

    self.photoButton.backgroundColor = [UIColor colorWithRed:0.34f green:0.11f blue:0.02f alpha:0.8f];

    [self.view addSubview:self.photoButton];

    

    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 64, 150, 50)];

    [button setTitle:@"换图片" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(onLick) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:button];

    

}

 

 

-(void)onLick{

    

    UIActionSheet * actionSheet = [[UIActionSheet alloc]initWithTitle:@"prompt" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"photo" otherButtonTitles:@"camera", nil];

    

    

    [actionSheet showInView:self.view];

}

 

//UIActionSheetDelegate

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    

    

    switch (buttonIndex) {

        case 0:

        {

//           选择相

            [self lookPhoto];

        }

            break;

        case 1:{

//           选择相机

            [self takeCamare];

        }

            break;

        default:

            break;

    }

    

}

//取相册

-(void)takeCamare{

    //资源类型为照相机

    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

    //判断是否有相机

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){

        UIImagePickerController *picker = [[UIImagePickerController alloc] init];

        picker.delegate = self;

        //设置拍照后的图片可被编辑

        picker.allowsEditing = YES;

        //资源类型为照相机

        picker.sourceType = sourceType;

        [self presentModalViewController:picker animated:YES];

        

    }else {

        

        UIAlertView * alertView1 = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备无摄像头,请调用真机" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];

        [alertView1 show];

        //        NSLog(@"该设备无摄像头");

    }

 

    

    

}

-(void)lookPhoto{

    UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];

/*

 UIImagePickerControllerSourceTypePhotoLibrary,

 UIImagePickerControllerSourceTypeCamera,

 UIImagePickerControllerSourceTypeSavedPhotosAlbum

 */

// UIImagePickerControllerSourceTypeSavedPhotosAlbum,有时会崩有时不会奔溃,

//    UIImagePickerControllerSourceTypePhotoLibrary,较稳定

//    打开图库类型

    imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

    imagePicker.delegate = self;

//    设置选择后的图片可被编辑

    imagePicker.allowsEditing = YES;

    [self presentViewController:imagePicker animated:YES completion:nil];

    

}

//UIImagePickerControllerDelegate

//图片选择器的委托方法,选择完后回调该方法,将图片的格式返回回来,显示在界面上。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo{

    

//    当图片不为空时显示图片并保存图片

    if (image != nil) {

        

        [self.photoButton setBackgroundImage:image forState:UIControlStateNormal];

//        将保存文件到沙河路径下

//        把图片转换成NSData类型的数据来保存文件

        NSData * data;

//        判断图片是不是;png格式的文件

        if (UIImagePNGRepresentation(image)) {

//         返回png图像

            data = UIImagePNGRepresentation(image);

        

        }else{

//            返回jpeg图像

            data = UIImageJPEGRepresentation(image, 1.0);

        }

        

//

        [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];

        

    }

//    关闭相册界面

    [picker dismissViewControllerAnimated:YES completion:^{

        

    }];

    

}

 

posted @ 2016-01-07 17:09  小飞侠的博客  阅读(1536)  评论(0)    收藏  举报