IOS 拍照和选取照片

控制器遵循代理 UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate


-(void)doChoosePhoto
{
    NSLog(@"------弹出相册");
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"本地相册", @"用户相机", nil];
    [actionSheet showInView:self.view];
}

#pragma mark UIActionSheetDelegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        if (buttonIndex == 2) {
            return;
        }
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        
        
        imagePicker.delegate =self;
        if (buttonIndex == 0) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        } else if(buttonIndex == 1) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        [self presentViewController:imagePicker animated:YES completion:nil];
    }];
}
#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *parentImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    //self.headView.HimageView.image =parentImage;
    
    // 调用下面的保存方法
    //[self saveImage:parentImage withName:@"currentImage.png"];
    
    //判断是否修改了头像
    NSData *imgData = UIImageJPEGRepresentation(parentImage, 0.5);
    
    SEHomePicCell *cell=[self.tabView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.picImageVew.image=parentImage;
    
    
//    [ZYTool saveImageData:imgData];
//
//    self.tabheadView.HimageView.image=[ZYTool getHeadImagew];//[UIImage imageWithData:imgData];
//
//    if (imgData.length > 0) {
//        //文件流上传
//        [self upDateHeadfile:imgData];
//    }
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

你可能感兴趣的:(IOS 拍照和选取照片)