选择的图像不显示

时间:2015-04-10 07:41:51

标签: ios objective-c uiimagepickercontroller

我已经构建了一个相机和库应用程序,我从库中选择图像并将其显示到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的地方,但我没有说明错误。

3 个答案:

答案 0 :(得分:0)

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

尝试放置

myImage.image = chosenImage;

这一行之后:

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

答案 1 :(得分:0)

您无法在创建对象之前对其进行分配,因此只需将代码更改为

即可
- (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];

}

答案 2 :(得分:0)

这是因为在将所选照片添加到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; }