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:@"user_photo@2x.png"] 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];  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值