【问题标题】:UIImagePicker set more than one image viewUIImagePicker 设置多个图像视图
【发布时间】:2012-07-23 11:38:06
【问题描述】:

我有几个按钮。当按下任一按钮时,它会弹出并 alertView 要求用户拍照或从相机胶卷中选择。现在我遇到的问题是我有 12 个按钮和 12 个 UIImageViews。所有按钮都有自己的动作,可以弹出警报并允许用户选择任一选项。现在在 didFinishPickingMediaWithInfo 方法中,我将图像从相机或相机胶卷传递到第一个 imageView。这一切都很好,但是如果我想选择按钮 2,它会触发另一个带有另一个标签的警报,我想设置 imageView 2 等等(不替换 imageView1)。我需要一种方法在 didFinishPickingMediaWithInfo 中根据弹出警报的按钮选择来区分要设置的 imageView。因为目前第一个图像视图只有在我选择另一个应该设置相应图像的按钮时才会设置和重置。

这是按钮的操作。

-(IBAction) addPhoto1:(id) sender {

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Image Source" message:@"Take a photo or select a previously taken photo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Take Photo", @"Select Photo", nil];
    [alert show];
    alert.tag = 101;
    [alert release];

}

并提醒clickedButtonAtIndex

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{

    if (alert.tag == 101) {

        if (buttonIndex == 1) {
            //Take photo
            [self performSelector:@selector(takePicture:) withObject:nil afterDelay:0.0];
        }
        else if (buttonIndex == 2){
            //Camera roll
            [self performSelector:@selector(pictureAlbum:) withObject:nil afterDelay:0.0];
        }
        else if (buttonIndex == 0) {
            NSLog(@"Cancel");
        }
    }
}

这是didFinishPickingMediaWithInfo

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

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {

        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];


        [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:dict completionBlock:nil];


        if (addFirstImage.tag == 1001) {
            firstImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
           firstImage.layer.cornerRadius = 5;
           firstImage.layer.masksToBounds = YES;

        }
        if (addSecondImage.tag == 1002) {            
           secondImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
           secondImage.layer.cornerRadius = 5;
           secondImage.layer.masksToBounds = YES;

        }
    }
}

现在显然这是不对的,根据最初按下的警报按钮设置正确的 imageViews 图像的最佳方法是什么? (addFirstImage 和 addSecondImage 都是通过 IB 链接的按钮)

非常感谢

【问题讨论】:

    标签: ios uiimageview uibutton uiimagepickercontroller ibaction


    【解决方案1】:

    你为什么不使用动作的“发送者”来“记住”哪个按钮首先被按下? 使用 IBOutletCollections,以相同的索引排序:

    @property (...) IBOutletCollection(UIButton) allYourButtons;
    
    @property (...) IBOutletCollection(UIImageView) allYourImageViews;
    // I suppose that both Collections objects have 'tags' (set in XIB file)
    // a UIButton and the corresponding UIImageVIew have the SAME tag
    
    @property (...) NSInteger clickedButtonIndex;
    
    - (IBAction) imageViewWasPressed:(id)sender
    {
        //keep a reference to the specific button that launched the action
        self.clickedButtonIndex = [(UIButton *)sender tag]; 
    
        [self presentAlertViewForPictureChoice];
    }
    

    然后,您在 UIAlertView 委托方法中使用 clickedButtonIndex 来查找来自 allYourImageViews 的哪个 UIImageVIew 应该更新其图像...

    - (UIImageView *)imageToUpdateForSelectedIndex:(NSInteger)anIndex
    {
        UIImageView *result = nil;
        for (UIImageView *imageView in self.allYourImageViews){
            if(imageView.tag == anIndex){
                 result = imageView;
                break;
            }
        }
        return result;
    
    }
    

    (编辑) 好的,将 UIButton 与 UIImageView 关联的另一种方法是创建一个子类

    // a new UIButton subclass
    
    ButtonWithImageView : UIButton
    
    @property (...) IBOutlet UIImageView *relatedImageView;
    
    //IBOutlet enables you to directly link a Button with its imageVIew in your XIB
    
    // in your controller
    
    @property (...) ButtonWithImageView *selectedButton;
    
    
    - (IBAction)buttonWasPressed:(id)sender
    {
        ...
        self.selectedButton = (ButtonWithImageView *)sender;
    
    }
    

    那么你所要做的就是编辑self.selectedButton.relatedImageView

    【讨论】:

    • 嗯不确定..我只需要一种方法来知道按下了哪个按钮,以便它在 didfinishpickingmediawithinfo 中设置正确的图像视图。类似 if (action1 was fire ) then set image view one if (action2 was fire) 然后设置图像视图 2 等等。
    【解决方案2】:

    好的,我想出了如何做到这一点。首先,我在接口文件中创建了一个名为 myTag 的 ivar int

    int myTag;
    

    然后我在 viewDidLoad 中用标签号标记了我的所有按钮,即 button1.tag = 1001(我的 12 个按钮为 1012),

    然后,我为每个按钮创建了一个 Action,而不是 12 个。并通过 IB 将所有内容连接到这一操作。

    - (IBAction)takePhoto:(UIButton*)sender {
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Image Source" message:@"Take a photo or select a previously taken photo" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Take Photo", @"Select Photo", nil];
    [alert show];
    alert.tag = 101;
    [alert release];
    
    if (sender.tag == 1001){
    
        NSLog(@"You clicked button 1");
        myTag = 1001;
    
    }
    
    if (sender.tag == 1002){
    
        NSLog(@"You clicked button 2");
        myTag = 1002;
    }
    

    }

    然后我检测按下了什么按钮并将一个 int 值传递给 myInt ivar

    然后在我的 didFinishPickingMediaWithInfo 中执行此操作!

    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
    
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    
        [library writeImageToSavedPhotosAlbum:[image CGImage] metadata:dict completionBlock:nil];
    
        if (myTag == 1001){
    
    
            firstImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
            firstImage.layer.cornerRadius = 5;
            firstImage.layer.masksToBounds = YES;
    
        }
    
        if (myTag == 1002) {
            secondImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
            secondImage.layer.cornerRadius = 5;
            secondImage.layer.masksToBounds = YES;
        }
    
    
    
    
    
        [picker dismissModalViewControllerAnimated:YES];
    
    }
    

    一切正常!非常感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多