怎么使uiimagepickercontroller在window上展示

 我来答
受司大人
2015-02-19 · 知道合伙人影视综艺行家
受司大人
知道合伙人影视综艺行家
采纳数:20262 获赞数:171846
毕业于浙江广厦,有一定的电脑专业基础和两年工作经验,读过相关书籍多本

向TA提问 私信TA
展开全部
1。 定义 类变量:
UIImagePickerController* picker_library_;

2。实现 UIImagePickerControllerDelegate 这个delegate

3。 以模态的方式,显示 图片选取器

[cpp] view plaincopy
picker_library_ = [[UIImagePickerController alloc] init];
picker_library_.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker_library_.allowsEditing = YES;
picker_camera_.allowsImageEditing=YES;
picker_library_.delegate = self;
[self presentModalViewController: picker_library_
animated: YES];

其中,sourceType 指定了 几种 图片的来源:
UIImagePickerControllerSourceTypePhotoLibrary:表示显示所有的照片
UIImagePickerControllerSourceTypeCamera:表示从摄像头选取照片
UIImagePickerControllerSourceTypeSavedPhotosAlbum:表示仅仅从相册中选取照片。

allowEditing和allowsImageEditing 设置为YES,表示 允许用户编辑图片,否则,不允许用户编辑。

4。 当用户选择一个图片以后,有可能调用两种不同的函数,根据版本的不同。所以,如果要同时支持高版本和低版本的兼容性,那么就
要处理两种函数。

//3.x 用户选中图片后的回调
- (void)imagePickerController: (UIImagePickerController *)picker
didFinishPickingMediaWithInfo: (NSDictionary *)info

//2.x 用户选中图片之后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

两种函数的处理代码如下:

[cpp] view plaincopy
//3.x 用户选中图片后的回调
- (void)imagePickerController: (UIImagePickerController *)picker
didFinishPickingMediaWithInfo: (NSDictionary *)info
{
if (picker == picker_camera_)
{
//如果是 来自照相机的image,那么先保存
UIImage* original_image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
UIImageWriteToSavedPhotosAlbum(original_image, self,
@selector(image:didFinishSavingWithError:contextInfo:),
nil);
}

//获得编辑过的图片
UIImage* image = [info objectForKey: @"UIImagePickerControllerEditedImage"];

[self dismissModalViewControllerAnimated:YES];
[picker release];
}

[cpp] view plaincopy
//2.x 用户选中图片之后的回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSMutableDictionary * dict= [NSMutableDictionary dictionaryWithDictionary:editingInfo];

[dict setObject:image forKey:@"UIImagePickerControllerEditedImage"];

//直接调用3.x的处理函数
[self imagePickerController:picker didFinishPickingMediaWithInfo:dict];
}

5。 用户取消选择

[cpp] view plaincopy
// 用户选择取消
- (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式