相机拍照与图库

首先要采用三个协议,分别是 UINavigationControllerDelegate , UIImagePickerControllerDelegate , UIActionSheetDelegate


-(IBAction)photoClick:(id)sender{

    UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"图库"nil];

    [actionSheet showInView:self.view];

}


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

    if (buttonIndex == 0) {//拍照

        //资源类型为照相机

        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

        //判断是否有相机

        if ([UIImagePickerControllerisSourceTypeAvailableUIImagePickerControllerSourceTypeCamera]){

            UIImagePickerController *picker = [[UIImagePickerControllerallocinit];

            picker.delegate = self;

            //资源类型为照相机

            picker.sourceType = sourceType;

            [selfpresentViewController:picker animated:YEScompletion:nil];

        }else {

            UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"该设备无摄像头"delegate:selfcancelButtonTitle:@"取消"otherButtonTitlesnil];

            [alertView show];

        }

    }elseif (buttonIndex == 1){//图库

        UIImagePickerController *pickerController = [[UIImagePickerControllerallocinit];

        pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        pickerController.delegate = self;

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

        pickerController.allowsEditing = NO;

        [selfpresentViewController:pickerController animated:YEScompletion:nil];

    }

}


-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    //当选择的类型是图片

    if ([type isEqualToString:@"public.image"])

    {

        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        UIImageView * tempImage = [[UIImageViewallocinitWithImage:image];

        tempImage.frame = CGRectMake(20+picWidth+20topImgHeight+timeImgHeight+21UISCREEN_WIDTH-(20+picWidth+20)-20UISCREEN_HEIGHT/2-topImgHeight-timeImgHeight-50);

        tempImage.userInteractionEnabled = NO;

        [self.viewaddSubview:tempImage];

    }

    [picker dismissViewControllerAnimated:YEScompletion:nil];

}





相机拍照与图库_第1张图片

你可能感兴趣的:(UIActionSheet)