0

我有 2UIButton秒,我希望两个按钮都可以选择图像,button1正在将图像设置为imageview1和。我现在创建了它选择一个图像并设置为该图像,但是如果我正在创建,我现在不需要做什么:button2imageView2button1imageView1button2

- (IBAction)chooseImage1:(id)sender {
     imagePicker.delegate = self;
     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     [self presentViewController:imagePicker animated:YES completion:nil];
}

- (IBAction)chooseImage2:(id)sender {
     imagePicker.delegate = self;
     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     [self presentViewController:imagePicker animated:YES completion:nil];
}

UIImage *image;

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

      NSURL *mediaURL;
      mediaURL = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
      image = (UIImage *)[info valueForKey:UIImagePickerControllerOriginalImage];

     imageView1.image=image;
     [picker dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
     [picker dismissViewControllerAnimated:YES completion:nil];
}
4

1 回答 1

1

.h在您的文件中创建索引。就像是

NSUInteger *selectedImageIndex;

在你的.m文件中:

- (void)showImagePicker:(UIImagePickerControllerSourceType)source{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = source;
    ipc.allowsEditing = YES;
    ipc.delegate = self;
    [self presentModalViewController:ipc animated:YES];
}

- (IBAction)chooseImage1:(id)sender {
    selectedImageIndex = 1;
    [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (IBAction)chooseImage2:(id)sender {
    selectedImageIndex = 2;
    [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}    

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        UIImage *img = [info objectForKey:@"UIImagePickerControllerEditedImage"];
        if (!img)
           img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        if (selectedImageIndex == 1)
           imageView1.image=img;
        else
           imageView2.image = img;

        [picker dismissViewControllerAnimated:YES completion:nil];
}


-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
       [picker dismissViewControllerAnimated:YES completion:nil];
}

但话又说回来,这只是一种方法。

于 2013-04-18T19:48:32.790 回答