UIImagePicker设置了多个图像视图

时间:2012-07-23 11:38:07

标签: ios uiimageview uibutton uiimagepickercontroller ibaction

我有几个按钮。当按下其中任何一个按钮时,它会弹出并且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链接的按钮)

非常感谢

2 个答案:

答案 0 :(得分:0)

为什么不使用动作的'发送者'来“记住”先按下哪个按钮? 使用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];
}

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

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

}

(适用EDIT) 好的,另一种将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

即可

答案 1 :(得分:0)

好的,我想出了如何做到这一点。首先,我在接口文件

中创建了一个名为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];

}

一切都完美!非常感谢你的帮助!