2014-03-02 130 views
0

我想在我的视图控制器中使自定义ImagePicker(相机)。自定义UIImagePickerController视图不工作

下面的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Instantiate the UIImagePickerController instance 
     self.picker = [[UIImagePickerController alloc] init]; 

     // Configure the UIImagePickerController instance 
     self.picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     self.picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; 
     self.picker.cameraDevice = UIImagePickerControllerCameraDeviceRear; 
     self.picker.showsCameraControls = NO; 

     // Make us the delegate for the UIImagePickerController 
     self.picker.delegate = self; 

     // Set the frames to be full screen 
     CGRect screenFrame = [[UIScreen mainScreen] bounds]; 
     self.view.frame = screenFrame; 
     self.picker.view.frame = screenFrame; 

     // Set this VC's view as the overlay view for the UIImagePickerController 
     self.picker.cameraOverlayView = self.view; 
    } 
    return self; 
} 

我有什么是我创建的按钮,仅此而已黑色页面。当我按下拍照按钮时,它确实拍摄了一张真实照片。 所以我的问题是UIImagePicker没有在我的视图中显示相机捕获。

任何人都可以帮助我请。 在此先感谢。

回答

0

确保您使用适当的委托方法在拍摄后显示图像。事情是这样的:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
NSData *myPhotoData = [[NSData alloc] init]; 
myPhotoData = UIImageJPEGRepresentation(image, 0.5); // compressionQuality between 0.0 and 1.0 higher being less compression 
myPhotoOutlet.image = [UIImage imageWithData:myPhotoData]; // myPhotoOutlet is an UIImageView outlet on your screen 
[self dismissViewControllerAnimated:YES completion:nil]; 
} 
+0

我已经做到了!我的问题是当我把VC视图设置为UIImagePickerController的Overlay时。没有UiImagePickerController,我得到了VC视图。所以当我停用这个时,UIImagePickeController像往常一样工作。 – Ali

0

,你必须在你的.h文件中添加一个委托声明:

@interface YourViewcontroller : UIViewController <UIImagePickerControllerDelegate> 

,你必须实现在.m文件的委托方法,让您的形象:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    [picker dismissModalViewControllerAnimated:YES]; 
    UIImage *newImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    // and then you affect your image in your image view 
    UIImageView *yourImageView= [[UIImageView alloc] initWithFrame:CGRectMake(0,0,300,300)]; 
    yourImageView.image=newImage; 
} 

我希望能帮到你!

+0

我已经做到了!我的问题是当我把VC视图设置为UIImagePickerController的Overlay时。没有UiImagePickerController,我得到了VC视图。所以当我停用这个时,UIImagePickerController像往常一样工作。 – Ali

相关问题