简体   繁体   English

如何为同一视图中的每个UIImageView分配从相机拍摄的照片?

[英]how I can assign for each UIImageView in the same view a picture taken from camera?

I have two UIImageView in the same view.for each imageView I should assign a picture taken from the camera or photos library. 我在同一个视图中有两个UIImageView。对于每个imageView,我应该分配一个从相机或照片库拍摄的图片。 When I start by doing that, I get the same photo for the two imageView. 首先,我得到两个imageView的同一张照片。

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

    [picker dismissViewControllerAnimated:YES completion:nil];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

} 


-(IBAction)addPhoto:(id)sender{

    UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:@"" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"Choose Photo" otherButtonTitles:@"Take Photo ", nil];


    [actionSheet showInView:self.view];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    shouldUpdateFirstImage =YES;
    // chosenImage = info[UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:nil];

  if (shouldUpdateFirstImage) {
        imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        shouldUpdateFirstImage = NO;
    }
    else {
        pictureView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        shouldUpdateFirstImage = YES;
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Put Picture"])
    {
        alrtView.hidden = YES;
        pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
       [self.view addSubview:pictureView];
        [self addPhoto:self];
    }

And the viewController.h 还有viewController.h

@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,UITextViewDelegate>
{

    BOOL shouldUpdateFirstImage;

}

The first ImageView is the background.Through an AlertView I choose to add Picture to the first ImageView.After that I recall the same actionsheet to choose the taken photo. 第一个ImageView是背景。通过AlertView,我选择将图片添加到第一个ImageView。之后,我调出相同的操作表来选择拍摄的照片。

TL;DR you need to track the state of your class, and have your delegate method respond accordingly to that state. TL; DR您需要跟踪类的状态,并让您的委托方法对状态进行相应的响应。

It sounds like you need your class to keep track of how many images have been displayed, and load new selections based on that information. 听起来您需要您的班级来跟踪已显示的图像数量,并根据该信息加载新的选择。 We call this "state", and you have to implement the logic for your imagePickerController delegate method to respond appropriately to that state. 我们将其称为“状态”,并且您必须为imagePickerController委托方法实现逻辑以适当地响应该状态。

One possible approach would be to have a BOOL scoped to your class (either an ivar or property will do, but ivar is likely more appropriate), then use it's value to keep track of which imageView should be updated. 一种可能的方法是将BOOL范围限定在您的类上(可以使用ivar或属性,但使用ivar可能更合适),然后使用其值来跟踪应该更新哪个imageView。

Assuming you already declared a BOOL named shouldUpdateFirstImage, you could do something like this: 假设您已经声明了一个名为shouldUpdateFirstImage的BOOL,则可以执行以下操作:

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

[picker dismissViewControllerAnimated:YES completion:nil];

if (shouldUpdateFirstImage) {
        imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        shouldUpdateFirstImage = NO;
    }
else {
    imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    shouldUpdateFirstImage = YES;
    }  
}

This would alternate which imageView was updated with each successive selection. 这将交替使用每个连续的选择更新哪个imageView。

Try this 尝试这个

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
      [picker dismissViewControllerAnimated:YES completion:nil];

 if(imageView.image)
    imageView2.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
 else
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用相机拍摄的照片在uiimageview中不会调整大小 - picture taken with camera doesn't resize in uiimageview 如何将图片分配给UIImageView(Swift 3) - How to assign a picture to the UIImageView (Swift 3) 如何使用摄像机的AVCaptureVideoPreviewLayer将流延迟到UIImageview? - How Can I delay the stream to UIImageview using AVCaptureVideoPreviewLayer from camera? UIImageview not updating once camera picture is taken but when gallery image is selected, camera picture shows for a second - UIImageview not updating once camera picture is taken but when gallery image is selected, camera picture shows for a second 从相机拍照后旋转UIImageView - UIImageView is rotated after taking picture from camera 从相机拍摄图片并将其显示在UIImageView中 - Taking a picture from the camera and show it in a UIImageView 如何缩小iPhone 7中相机拍摄的照片的大小 - How to reduce size of the picture taken by camera in iphone 7 如何在从图片库拍摄的uiimageview中显示拾取的图像到另一个视图控制器中的另一个uiimageview中? - How to display picked image in a uiimageview taken from the photo library to another uiimageview in a different view controller? Xcode:从直接放在UIImageView中的相机拍摄的图像 - Xcode: Image taken from camera directly placed in UIImageView Swift:编辑从相机拍摄的图像以填充UIImageView - Swift: Edit image taken from camera to fill a UIImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM