ios UIImagePickerController 添加一个自定义的view

 

1. 在摄像头捕获的画面上添加一个自定义的view。
使用iphoneSDK 3.1的新API:UIImagePickerController的新属性cameraOverView来添加一个自定义的view。

        - (IBAction)getCameraPicture: (id)sender {  

        UIImagePickerController* picker = [[UIImagePickerController alloc] init];  

        picker.delegate = self;  

        picker.sourceType = UIImagePickerControllerSourceTypeCamera;  

        //picker.allowsEditing = YES;  

        picker.showsCameraControls = NO;//关闭默认的摄像设备  

        //picker.toolbarHidden = YES;  

          

        //设定图像缩放比例  

        picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, 1.0, 1.0);  

          

        //添加自定义信息层  

        self.overView = [[OverlayViewConroller alloc] initWithNibName:@"OverlayViewConroller" bundle:nil WithCameraPicker:picker];  

        overView.view.backgroundColor = [UIColor clearColor];//设定透明背景色  

        picker.cameraOverlayView = overView.view;  

          

        //打开摄像画面作为背景  

        [self presentModalViewController:picker animated:YES];  

          

        [picker release];  

    }  

 


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

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

    - (UIButton*)createButton:(CGFloat) x withY:(CGFloat) y withTitle:(NSString*) title withPercent:(CGFloat)percent {  

        UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ballon.size.width * percent, ballon.size.height * percent)];  

        [btn setCenter:CGPointMake(x, y)];  

        btn.autoresizingMask = UIViewAutoresizingNone;  

        [btn setBackgroundImage:ballon forState:UIControlStateNormal];  

        [btn setTitle:title forState:UIControlStateNormal];  

        [btn addTarget:self action:@selector(tagClick:) forControlEvents:UIControlEventTouchUpInside];  

        return btn;  

          

    }  

 

 

4. 点击view上的标记点,弹出描述详细情报的信息框,比如文字加上图片。
因为在iphone的应用中同时只能有一个窗体画面,所以只能采用弹出对话框来显示了,默认的对话框只能显示文字描述,要想显示图片,就需要改造对话框, 方法是让类实现协议< UIAlertViewDelegate>,重写方法- (void) willPresentAlertView:(UIAlertView*) alertView ;在这个方法里添加UIImageView来显示图片,改变对话框的大小,以及按钮的位置。

    - (void)tagClick:(id)sender {  

        //[picker takePicture];  

        UIAlertView* infoView = [[UIAlertView alloc]   

                              initWithTitle:@"Info"   

                              message:@"some thing is done"   

                              delegate:self   

                              cancelButtonTitle:@"Close"   

                              otherButtonTitles:nil];  

        [infoView show];  

        [infoView release];  

    }  

 


5. 在详细信息中播放视频
由于iphone未提供在任意控件内播放视频的功能,所以只能在详细表示画面添加视频播放的按钮,调用MPMoviePlayerController的 play方法来播放视屏,MPMoviePlayerController的初始化方法使用initWithContentURL方法加载视频播放的路径 URL

- (void) playMovie {  

    MPMoviePlayerController* mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];  

    if (mp) {  

        self.moviePlayer = mp;  

        [mp release];  

    }  

      

    [self.moviePlayer play];  

} 

 

6. 加载本地文件的路径URL

由于iphone在运行时有一个临时的运行环境,需要使用[NSBundle mainBundle]取得一个主Bundle,使用NSBundle的方法pathForResource:@"Movie" ofType:@"m4v"来生成一个本地运行时的文件路径。

    - (NSURL*)localMovieURL {  

        if (self.movieURL == nil) {  

            NSBundle* bundle = [NSBundle mainBundle];  

            if (bundle) {  

                NSString* moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];  

                if (moviePath) {  

                    self.movieURL = [NSURL fileURLWithPath:moviePath];  

                }  

            }  

        }  

        return self.movieURL;  

    }  

 


7. 让画面中的按钮view随拍摄方位的变化而移动
让画面中的view的移动,是通过设定UIButton的属性transform来实现的,需要使用[UIView beginAnimations:nil context:nil];启动一个动画环境,设定动画的动作时间以及延迟,通过[UIView commitAnimations];提交动画,transform是通过CGAffineTransformMakeTranslation(x, y)的类来生成,其中x为x方向需要移动的相对距离,y为y方向上需要移动的相对距离。

    - (void)moveButton:(UIButton*)button withOffsetX:(NSInteger)x andOffsetY:(NSInteger)y {  

        [UIView beginAnimations:nil context:nil];  

        [UIView setAnimationDuration:0.0];  

        [UIView setAnimationDelay:0.0];  

        CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);  

        button.transform = transform;  

        [UIView commitAnimations];  

    }  

 

   
8. 让画面中的按钮view随距离远近而改变按钮view大小
取得当前按钮view 的frame.size,重新设定其width和height,把frame设定回按钮view就可以改变其大小了,可能有通过动画实现的方法,我暂时还未发现。

    - (void)scaleButton:(UIButton*)button withOffsetX:(CGFloat)x andOffsetY:(CGFloat)y {  

        CGRect frame = button.frame;  

        frame.size.width = x;  

        frame.size.height = y;  

        button.frame = frame;  

    }  

 

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

你可能感兴趣的:(ios UIImagePickerController 添加一个自定义的view)