2012-06-25 80 views
0

我想要一個攝像頭視圖,將圖像捕捉到本地文件或讓用戶從本地照片庫中選擇圖像。我想也許有人爲此寫了好的庫/代碼。也許我可以利用它。有沒有好的嗎?謝謝。我只是避免重新發明輪子:)iOS相機捕獲視圖已經?

回答

3

UIImagePickerController內置到iOS和非常容易使用,它允許所有功能你想要的。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

爲用戶拍攝新照片:

UIImagePicker *imagePicker

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) 
{ 
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    imagePicker.delegate = self; 
    imagePicker.allowsEditing = NO; 
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; 
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self presentModalViewController:imagePicker animated:YES]; 
} 

,或選擇從設備的現有照片:

if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) return; 

imagePicker.delegate = self; 
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
imagePicker.allowsEditing = NO; 

[self presentModalViewController:imagePicker animated:YES]; 

要一定要包括這個!

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    [self dismissModalViewControllerAnimated:YES]; 
}