ios UIImagePickerController 添加一个自定义的view。

1. 在摄像头捕获的画面上添加一个自定义的view。
使用iphoneSDK 3.1的新API:UIImagePickerController的新属性cameraOverView来添加一个自定义的view。
C代码   收藏代码
  1.     - (IBAction)getCameraPicture: (id)sender {  
  2.     UIImagePickerController* picker = [[UIImagePickerController alloc] init];  
  3.     picker.delegate = self;  
  4.     picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
  5.     //picker.allowsEditing = YES;  
  6.     picker.showsCameraControls = NO;//关闭默认的摄像设备  
  7.     //picker.toolbarHidden = YES;  
  8.       
  9.     //设定图像缩放比例  
  10.     picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, 1.0, 1.0);  
  11.       
  12.     //添加自定义信息层  
  13.     self.overView = [[OverlayViewConroller alloc] initWithNibName:@"OverlayViewConroller" bundle:nil WithCameraPicker:picker];  
  14.     overView.view.backgroundColor = [UIColor clearColor];//设定透明背景色  
  15.     picker.cameraOverlayView = overView.view;  
  16.       
  17.     //打开摄像画面作为背景  
  18.     [self presentModalViewController:picker animated:YES];  
  19.       
  20.     [picker release];  
  21. }  


2. 在自定义的view上添加标志点图标。
一种方法是在view的- (void)drawRect:(CGRect)rect;方法里面添加图像的绘制。
另一种方法是新建一个按钮view,设定背景图片,利用addSubView的方法添加到当前view中,本应用中采用此方法。

3. 对动态添加的按钮绑定UIControlEventTouchUpInside事件关联
可以利用UIButton的方法 addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside来绑定需要的事件。

C代码   收藏代码
  1. - (UIButton*)createButton:(CGFloat) x withY:(CGFloat) y withTitle:(NSString*) title withPercent:(CGFloat)percent {  
  2.     UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ballon.size.width * percent, ballon.size.height * percent)];  
  3.     [btn setCenter:CGPointMake(x, y)];  
  4.     btn.autoresizingMask = UIViewAutoresizingNone;  
  5.     [btn setBackgroundImage:ballon forState:UIControlStateNormal];  
  6.     [btn setTitle:title forState:UIControlStateNormal];  
  7.     [btn addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside];  
  8.     return btn;  
  9.       
  10. }  


4. 点击view上的标记点,弹出描述详细情报的信息框,比如文字加上图片。
因为在iphone的应用中同时只能有一个窗体画面,所以只能采用弹出对话框来显示了,默认的对话框只能显示文字描述,要想显示图片,就需要改造对话框,方法是让类实现协议< UIAlertViewDelegate>,重写方法- (void) willPresentAlertView:(UIAlertView*) alertView ;在这个方法里添加UIImageView来显示图片,改变对话框的大小,以及按钮的位置。
C代码   收藏代码
  1. - (void)tagClick:(id)sender {  
  2.     //[picker takePicture];  
  3.     UIAlertView* infoView = [[UIAlertView alloc]   
  4.                           initWithTitle:@"Info"   
  5.                           message:@"some thing is done"   
  6.                           delegate:self   
  7.                           cancelButtonTitle:@"Close"   
  8.                           otherButtonTitles:nil];  
  9.     [infoView show];  
  10.     [infoView release];  
  11. }  


5. 在详细信息中播放视频
由于iphone未提供在任意控件内播放视频的功能,所以只能在详细表示画面添加视频播放的按钮,调用MPMoviePlayerController的play方法来播放视屏,MPMoviePlayerController的初始化方法使用initWithContentURL方法加载视频播放的路径URL
C代码   收藏代码
  1. - (void) playMovie {  
  2.     MPMoviePlayerController* mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];  
  3.     if (mp) {  
  4.         self.moviePlayer = mp;  
  5.         [mp release];  
  6.     }  
  7.       
  8.     [self.moviePlayer play];  
  9. }  


6. 加载本地文件的路径URL
由于iphone在运行时有一个临时的运行环境,需要使用[NSBundle mainBundle]取得一个主Bundle,使用NSBundle的方法pathForResource:@"Movie" ofType:@"m4v"来生成一个本地运行时的文件路径。
C代码   收藏代码
  1. - (NSURL*)localMovieURL {  
  2.     if (self.movieURL == nil) {  
  3.         NSBundle* bundle = [NSBundle mainBundle];  
  4.         if (bundle) {  
  5.             NSString* moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];  
  6.             if (moviePath) {  
  7.                 self.movieURL = [NSURL fileURLWithPath:moviePath];  
  8.             }  
  9.         }  
  10.     }  
  11.     return self.movieURL;  
  12. }  


7. 让画面中的按钮view随拍摄方位的变化而移动
让画面中的view的移动,是通过设定UIButton的属性transform来实现的,需要使用[UIView beginAnimations:nil context:nil];启动一个动画环境,设定动画的动作时间以及延迟,通过[UIView commitAnimations];提交动画,transform是通过CGAffineTransformMakeTranslation(x, y)的类来生成,其中x为x方向需要移动的相对距离,y为y方向上需要移动的相对距离。
C代码   收藏代码
  1. - (void)moveButton:(UIButton*)button withOffsetX:(NSInteger)x andOffsetY:(NSInteger)y {  
  2.     [UIView beginAnimations:nil context:nil];  
  3.     [UIView setAnimationDuration:0.0];  
  4.     [UIView setAnimationDelay:0.0];  
  5.     CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);  
  6.     button.transform = transform;  
  7.     [UIView commitAnimations];  
  8. }  

   
8. 让画面中的按钮view随距离远近而改变按钮view大小
取得当前按钮view 的frame.size,重新设定其width和height,把frame设定回按钮view就可以改变其大小了,可能有通过动画实现的方法,我暂时还未发现。
C代码   收藏代码
  1. - (void)scaleButton:(UIButton*)button withOffsetX:(CGFloat)x andOffsetY:(CGFloat)y {  
  2.     CGRect frame = button.frame;  
  3.     frame.size.width = x;  
  4.     frame.size.height = y;  
  5.     button.frame = frame;  
  6. }  

原创文章,欢迎转载,转载时务必注明原文地址及作者: cwh643  http://chenweihuacwh.iteye.com/blog/546126

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
iOS Objective-C 中自定义相片拍照功能,可以使用系统提供的 UIImagePickerController 类。UIImagePickerController一个系统自带的 UIImagePickerController 控制器,它提供了相机和相册的访问功能,可以方便地实现自定义相片拍照功能。 以下是简单的实现步骤: 1. 导入 UIImagePickerController 类: ``` #import <UIKit/UIKit.h> ``` 2. 创建 UIImagePickerController 实例: ``` UIImagePickerController *pickerController = [[UIImagePickerController alloc] init]; ``` 3. 配置 UIImagePickerController 实例: ``` pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; // 设置为相机模式 pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; // 设置为拍照模式 pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear; // 设置为后置摄像头 pickerController.allowsEditing = NO; // 禁止编辑 pickerController.delegate = self; // 设置代理 ``` 4. 打开相机: ``` [self presentViewController:pickerController animated:YES completion:nil]; ``` 5. 处理拍照结果: ``` - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *image = info[UIImagePickerControllerOriginalImage]; // 处理拍照结果 [picker dismissViewControllerAnimated:YES completion:nil]; } ``` 通过系统提供的 UIImagePickerController 控制器,我们可以轻松地实现自定义相片拍照功能。如果需要进一步定制相机界面和功能,可以考虑使用 AVFoundation 框架,自定义相机界面和拍照功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值