i os 获取相片

2.主要代码
(需要实现的协议UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate)
//选择图片

-(IBAction)btnChoosePhoto:(id)sender

{

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"选 择 图 片"

delegate:self

cancelButtonTitle:@"取消"

                                               destructiveButtonTitle:nil

otherButtonTitles:@"拍照",@"相册",@"图片库",nil];

actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

[actionSheet showInView: self.view];

[actionSheet release];

}

//UIActionSheet协议方法

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

{

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

    picker.delegate=self;

    picker.allowsEditing=YES;//是否允许编辑

    switch (buttonIndex)

    {

        case 0://照相机

        {

            if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])

            {

                picker.sourceType=UIImagePickerControllerSourceTypeCamera;

            }

            else

            {

                return;

            }

        }

            break;

        case 1://相册库

        {

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])

{

picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

}

        }

            break;

        case 2://相册

        {

            if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum])

            {

picker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;

            }

        }

            break;

            

        default:

        {

            return;

        }

            break;

    }

[self presentModalViewController:picker animated:YES];

[picker release];

}

//UIImagePickerController协议方法

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

{

    //[self.popOverController dismissPopoverAnimated:YES];

[picker dismissModalViewControllerAnimated:YES];

UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];//取得选择的图片

self.imageView.image=image;

    //[selectButton  setBackgroundImage: image forState: UIControlStateNormal];

}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

//[self.popOverController dismissPopoverAnimated:YES];

[picker dismissModalViewControllerAnimated:YES];

}

3.demo下载

http://dl.vmall.com/c0amopjpz1

你可能感兴趣的:(i os 获取相片)