怎么使uiimagepickercontroller在window上展示
1个回答
2015-02-19 · 知道合伙人影视综艺行家
关注
展开全部
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];
}
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];
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询