ios 使用UIImagePickerController 打开图片库和相机选择图片修改头像

转自 http://blog.csdn.net/jinglijun/article/details/8030544

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    //获取Documents文件夹目录    
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    
    NSString *documentPath = [path objectAtIndex:0];    
    //指定新建文件夹路径    
    NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"];    
    //创建ImageFile文件夹    
    [[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];    
    //保存图片的路径   
    self.imagePath = [imageDocPath stringByAppendingPathComponent:@"image.png"];  
     
}  
  
-(void)viewWillAppear:(BOOL)animated{  
    [super viewWillAppear:YES];  
    //根据图片路径载入图片  
    UIImage *image=[UIImage imageWithContentsOfFile:self.imagePath];  
    if (image == nil) {  
        //显示默认  
        [changeImg setBackgroundImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];  
    }else {  
        //显示保存过的  
        [changeImg setBackgroundImage:image forState:UIControlStateNormal];  
    }  
}  
  
- (void)dealloc {  
    [imagePath release];  
    [changeImg release];  
    [super dealloc];  
}  
- (IBAction)changeImage:(id)sender {  
    UIActionSheet *myActionSheet = [[UIActionSheet alloc]                      
                                    initWithTitle:nil  
                                    delegate:self  
                                    cancelButtonTitle:@"取消"  
                                    destructiveButtonTitle:nil  
                                    otherButtonTitles: @"从相册选择", @"拍照",nil];   
    [myActionSheet showInView:self.view];  
    [myActionSheet release];   
}  
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
    switch (buttonIndex) {  
        case 0:  
            //从相册选择  
            [self LocalPhoto];  
            break;  
        case 1:  
            //拍照  
            [self takePhoto];  
            break;  
        default:  
            break;  
    }  
}  
//从相册选择  
-(void)LocalPhoto{  
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];    
    //资源类型为图片库  
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
    picker.delegate = self;  
    //设置选择后的图片可被编辑  
    picker.allowsEditing = YES;  
    [self presentModalViewController:picker animated:YES];  
    [picker release];  
}  
  
//拍照  
-(void)takePhoto{  
    //资源类型为照相机  
    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];  
        [picker release];  
    }else {  
        NSLog(@"该设备无摄像头");  
    }  
}  
#pragma Delegate method UIImagePickerControllerDelegate    
//图像选取器的委托方法,选完图片后回调该方法    
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{  
      
    //当图片不为空时显示图片并保存图片  
    if (image != nil) {    
        //图片显示在界面上  
        [changeImg 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 dismissModalViewControllerAnimated:YES];  
}  

你可能感兴趣的:(iOS,UIKit,iOS,摄像头,图片,沙盒,imageview)