将拍摄的图像分配给图像view.image

时间:2015-10-06 06:28:40

标签: ios objective-c uiimageview uiimage

我有最初隐藏的图像视图。当我点击TakePhotoButton然后它拍摄图像并将其指定给图像视图。然后图像视图可见。

当我调试代码时,即使productImg不是nil,我的chosenImage也始终为零。

@property (strong, nonatomic) IBOutlet UIImageView *productImg;

- (void)imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info {

   UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
   self.productImg.hidden=NO;
   self.productImg.image=chosenImage;

   [picker dismissViewControllerAnimated:YES completion:NULL];

 }

3 个答案:

答案 0 :(得分:1)

试试这个:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    __block MyViewController *aBlockSelf = self; // Replace MyViewController with your View Controller

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

    [picker dismissViewControllerAnimated:YES completion:^{
        aBlockSelf.productImg.hidden = NO;
        aBlockSelf.productImg.image = chosenImage;
    }];
}

答案 1 :(得分:0)

您使用以下代码它可能对您有所帮助。

- (void)imagePickerController:(UIImagePickerController *)picker
 didFinishPickingMediaWithInfo:(NSDictionary *)info {
{
       UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
       self.productImg.hidden=NO;
       self.productImg.image=chosenImage;

       [picker dismissViewControllerAnimated:YES completion:NULL];
}

答案 2 :(得分:0)

为什么你最初必须隐藏它。因为图像将为null,所以不会看到它。

   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosenImage;

            chosenImage= info[UIImagePickerControllerOriginalImage];
            [self.productImg setImage:chosenImage];
            [picker dismissViewControllerAnimated:YES completion:NULL];

}