重定义 UIImagePickerController

本文探讨如何在iOS应用中实现类似Path的PhotoPicker功能,通过自定义代码移除原生的Cancel按钮,并加入从相册直接获取图片的按钮。详细介绍了如何通过查找和操作UI元素来实现这一效果,包括如何隐藏不必要的视图元素和自定义相机覆盖视图。同时,提供了一个案例展示如何通过框架图和代码示例来完成整个过程。

今天想实现一个类似Path 的Photo Picker的效果,没有Cancel按钮,取而代之的是添加一个从相册获取的按钮,要知道这在官方的SDK里面是没有。
开始之前,先做下功课,找到几个相关的文章

http://blog.airsource.co.uk/index.php/2008/11/11/views-of-uiimagepickercontroller/

http://www.cocoachina.com/bbs/read.php?tid-11664-page-1.html>
以下的这个框架图对接下来的工作会很有帮助
红色的高亮部分是从屏幕上看到的照片的预览图,除了这个之外,他上面的view都可以去掉。
要想编辑这些个View,得先找到他们,这里给出了个方法

#pragma mark get/show the UIView we want
//Find the view we want in camera structure.
-(UIView *)findView:(UIView *)aView withName:(NSString *)name{
    Class cl = [aView class];
    NSString *desc = [cl description];

    if ([name isEqualToString:desc])
        return aView;

    for (NSUInteger i = 0; i < [aView.subviews count]; i++)
    {
        UIView *subView = [aView.subviews objectAtIndex:i];
        subView = [self findView:subView withName:name];
        if (subView)
            return subView;
    }
    return nil;
}

怎么用,下面这段代码里给出了比较详细的例子

-(void)addSomeElements:(UIViewController *)viewController{
    //Add the motion view here, PLCameraView and picker.view are both OK
    UIView *PLCameraView=[self findView:viewController.view withName:@"PLCameraView"];
    [PLCameraView addSubview:touchView];//[viewController.view addSubview:self.touchView];//You can also try this one.

    //Add button for Timer capture
    [PLCameraView addSubview:timerButton];
    [PLCameraView addSubview:continuousButton];

    [PLCameraView insertSubview:bottomBarImageView atIndex:1];

    //Used to hide the transiton, last added view will be the topest layer
    [PLCameraView addSubview:myTransitionView];

    //Add label to cropOverlay
    UIView *cropOverlay=[self findView:PLCameraView withName:@"PLCropOverlay"];
    [cropOverlay addSubview:lblWatermark];

    //Get Bottom Bar
    UIView *bottomBar=[self findView:PLCameraView withName:@"PLCropOverlayBottomBar"];

    //Get ImageView For Save
    UIImageView *bottomBarImageForSave = [bottomBar.subviews objectAtIndex:0];

    //Get Button 0
    UIButton *retakeButton=[bottomBarImageForSave.subviews objectAtIndex:0];
    [retakeButton setTitle:@"重拍" forState:UIControlStateNormal];

    //Get Button 1
    UIButton *useButton=[bottomBarImageForSave.subviews objectAtIndex:1];
    [useButton setTitle:@"保存" forState:UIControlStateNormal];

    //Get ImageView For Camera
    UIImageView *bottomBarImageForCamera = [bottomBar.subviews objectAtIndex:1];

    //Set Bottom Bar Image
    UIImage *image=[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"BottomBar.png"]];
    bottomBarImageForCamera.image=image;
    [image release];

    //Get Button 0(The Capture Button)
    UIButton *cameraButton=[bottomBarImageForCamera.subviews objectAtIndex:0];
    [cameraButton addTarget:self action:@selector(hideTouchView) forControlEvents:UIControlEventTouchUpInside];

    //Get Button 1
    UIButton *cancelButton=[bottomBarImageForCamera.subviews objectAtIndex:1];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(hideTouchView) forControlEvents:UIControlEventTouchUpInside];
}

++++++++++++++++++++++++++++
兔子说可以这么实现:
先 picker.showsCameraControls=NO;
然后自己自定义 picker.cameraOverlayView
有待验证~
[picker.topViewController.navigationController pushViewController:controller animated:YES];

转 http://blog.cnrainbird.com/index.php/2012/03/14/zhong_ding_yi_-uiimagepickercontroller/ 

 

以下还有其它的资料:

http://blog.airsource.co.uk/index.php/2008/11/11/views-of-uiimagepickercontroller/ 

http://hi.baidu.com/lishiq820/item/81011ee084ed4015595dd822 

 

转载于:https://www.cnblogs.com/likwo/archive/2012/07/07/2580669.html

### UIImagePickerController 使用指南 #### 初始化与配置 `UIImagePickerController` 是用于显示媒体选择界面的标准控制器。为了初始化并展示这个控制器,通常会遵循如下模式: ```swift let picker = UIImagePickerController() picker.sourceType = .photoLibrary // 或者 .camera, .savedPhotosAlbum // 设置代理对象来处理用户的选择事件 picker.delegate = self as? UINavigationControllerDelegate & UIImagePickerControllerDelegate present(picker, animated: true, completion: nil) ``` 这里设置了 `sourceType` 属性指定图片来源可以是从相册选取还是拍照[^1]。 #### 实现协议方法 为了让 `UIImagePickerController` 正常工作,需要实现其委托协议中的两个主要的方法: - 当用户成功挑选了一张或多张图片时调用此函数: ```swift func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { dismiss(animated: true, completion: nil) guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return } } ``` - 如果取消操作,则执行另一个回调函数: ```swift func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { dismiss(animated: true, completion: nil) } ``` 这些方法允许应用程序响应用户的交互行为,并适当地关闭模态视图控制器[^2]。 #### 常见问题及其解决方案 有时开发者可能会遇到一些关于 `UIImagePickerController` 的挑战。以下是几个常见的例子以及如何克服它们的方式: - **旋转支持**: 默认情况下,`UIImagePickerController` 可能不会按照预期方式处理设备方向的变化。可以通过自定义导航栏或其他UI组件来自行管理布局适应不同屏幕方向[^3]。 - **性能优化**: 对于大型图像文件,在加载过程中可能会影响应用流畅度。考虑压缩所选图片或将处理推迟到后台线程来进行以提高用户体验。 - **权限请求失败**: 用户首次启动程序时需授权访问相机或相册;如果被拒绝则应提供友好的提示信息告知原因及解决办法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值