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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM