【问题标题】:Chosen Image Is Not Showing选择的图像未显示
【发布时间】:2015-04-10 07:41:50
【问题描述】:

我已经构建了一个相机和库应用程序,我在其中从库中选择图像并将其显示到 imageView。但是发生的问题是选择图像后没有在 imageView 中显示。

这是我的代码。

@interface ViewController ()
{
    BOOL isImageselected;
    UIImageView *myImage;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                          message:@"Device has no camera"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles: nil];

         [myAlertView show];

    }

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)addPhotoButton:(id)sender {

    [UIView animateWithDuration:0.3 animations:^{
    // animation here
    self.pickerView.frame = CGRectMake(0.0, 443.0, 320.0, 125.0);
    }];
}

- (IBAction)cameraButton:(id)sender {


[UIView animateWithDuration:0.3 animations:^{
    // animation here
    self.pickerView.frame = CGRectMake(0.0, 568.0, 320.0, 125.0);
}];

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];


}

- (IBAction)uploadImageButton:(id)sender {

    [UIView animateWithDuration:0.3 animations:^{
    // animation here
    self.pickerView.frame = CGRectMake(0.0, 568.0, 320.0, 125.0);
}];


    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:picker animated:YES completion:NULL];


}


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

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    myImage.image = chosenImage;
    isImageselected = YES;
    if (isImageselected == YES) {

        myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
        self.addPhotoButton.frame = CGRectMake(106.0, 8.0, 95.0, 95.0);
    }

    [picker dismissViewControllerAnimated:YES completion:NULL];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}



@end

我认为问题发生在我动态创建 imageView 的地方,但我没有明白问题所在。

【问题讨论】:

    标签: ios objective-c uiimagepickercontroller


    【解决方案1】:

    问题是您在创建 myImage 之前将图像分配给 UIImageView 对象 myImage。

    尝试放置线

    myImage.image = chosenImage;
    

    在这一行之后:

        myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
    

    【讨论】:

    • 没有发生。我也尝试在 viewDidLoad 中分配 imageView,但我认为 imageView 没有创建。 @弗拉基米尔
    • 你添加你的imageView来查看了吗? [self.view addSubview:myImage];
    • 糟糕!!!!那是主要问题。 @Vladimir 我必须努力学习。 :(我是个新手。:(
    • 别担心 :) 我们都去过那里。
    • 你能帮我做更多事情吗?如果我想从库中选择多个图像并将其一个接一个地放置在我的视图中,我该怎么做?根据我的代码,如果我选择图像,它将覆盖前一个。
    【解决方案2】:

    这是因为在将所选照片添加到 myImage 之后,您正在重新分配 myImage,这会创建 myImage 的新对象。试试这个

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    isImageselected = YES;
    if (isImageselected == YES) {
    
        myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
    ....
    myImage.image = chosenImage; }
    

    【讨论】:

      【解决方案3】:

      您不能在创建对象之前不分配对象,所以只需稍微修改一下代码即可

      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      
      
          isImageselected = YES;
          if (isImageselected == YES) {
      
              myImage = [[UIImageView alloc]initWithFrame:CGRectMake(8.0, 8.0, 95.0, 95.0)];
              [self.view addSubView:myImage];
              self.addPhotoButton.frame = CGRectMake(106.0, 8.0, 95.0, 95.0);
              UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
              myImage.image = chosenImage; 
         }
      
          [picker dismissViewControllerAnimated:YES completion:NULL];
      
      }
      

      【讨论】:

      • 不,伙计!我已经明白我错在哪里了。我已经纠正了它,但它没有发生。 imageView 没有创建。 @复古
      • 是的,因为您已经创建了 imageView,所以您必须将其添加为 self.view 的子视图
      猜你喜欢
      • 2016-01-21
      • 2011-08-21
      • 2017-10-17
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多