从相册简单选着图片

来源:互联网 发布:淘宝没法付款怎么办 编辑:程序博客网 时间:2024/06/09 02:06

                  使用UIImagePickerController能很简单的从相册中进行照片选着。只要创建UIImagePickerController后,就可以实现照片选着与相册相关的功能。首先要导入两个代理方法。这两个方法是

<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

则实现显示相册的代码是
UIImagePickerControllerSourceType sourceType=UIImagePickerControllerSourceTypePhotoLibrary;    if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {        UIImagePickerController*Picker=[[UIImagePickerController alloc]init];        Picker.delegate=self;        Picker.sourceType=sourceType;        [self presentViewController:Picker animated:YES completion:nil];    }

其中sourceType的属性有三种,你可以选着。但是,选着后要判断你的设备是否有该属性相关的设备

sourceType的属性UIImagePickerControllerSourceType功能说明                                                 UIImagePickerControllerSourceTypePhotoLibrary是从相册中打开照片选择画面UIImagePickerControllerSourceTypeCamera启动摄像头打开摄像画面UIImagePickerControllerSourceTypeSavedPhotosAlbum直接打开保存的照片列表。如果有摄像头设备,则打开相册。还有在界面跳转时,有的资料上是以

[self presentModalViewController:Picker animated:YES];

这个形式写的,但是,不是不可用,只是有警告。。。推荐使用上述代码里面的。。。

现在程序会调用置于delegate属性,实现

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

方法进行处理。注意:被选者的图片将以
UIImagePickerControllerOriginalImage

为建值从Info 中取得。。。代码如下:

首先创建一个接受图片的ImageView

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{    UIImageView*MyImageView;}@end

然后
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{                    UIImage*image=[info objectForKey:UIImagePickerControllerOriginalImage];    MyImageView.image=image;    ///[self dismissModalViewControllerAnimated:YES];不推荐使用,但也可以使用    [self dismissViewControllerAnimated:YES completion:nil];    }

注意:关闭相册也可以使用下面方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{    [self dismissViewControllerAnimated:YES completion:nil];}
















0 0
原创粉丝点击