让用户在4个不同的图像视图xcode上更改背景

时间:2012-11-19 18:45:47

标签: iphone xcode

我试图制作一个包含4个ImageView的应用程序,我想让用户通过按下每个ImageView下面的UIButton来更改不同ImageViews上的背景。我已经从一个教程中为此编写了一个代码,只显示了一个ImageView。我只是为每个按钮复制并粘贴了4次并更改了一些变量。但是当我运行它时,即使我按下第二,第三或第四个ImageView的UIButton,第一个ImageView也会改变它的图像。这里是代码:

#import "ViewController.h"

@interface ViewController ()
{
UIImagePickerController *imagePickerController;
UIImagePickerController *imagePickerController2;
UIImagePickerController *imagePickerController3;
UIImagePickerController *imagePickerController4;
}

@end

@implementation ViewController
@synthesize firstImageView, secondImageView, thirdImageView, fourthImageView;
- (void)viewDidLoad
{
[super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

- (IBAction)firstChangeButton:(id)sender
{
imagePickerController = [[UIImagePickerController alloc]init];
[imagePickerController setDelegate:self];
[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController animated:YES completion:nil];

}
- (IBAction)secondChangeButton:(id)sender
{
imagePickerController2 = [[UIImagePickerController alloc]init];
[imagePickerController2 setDelegate:self];
[imagePickerController2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController2 animated:YES completion:nil];

}
- (IBAction)thirdChangeButton:(id)sender
{
imagePickerController3 = [[UIImagePickerController alloc]init];
[imagePickerController3 setDelegate:self];
[imagePickerController3 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController3 animated:YES completion:nil];

}
- (IBAction)fourthChangeButton:(id)sender
{
imagePickerController4 = [[UIImagePickerController alloc]init];
[imagePickerController4 setDelegate:self];
[imagePickerController4 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagePickerController4 animated:YES completion:nil];

}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];

NSData *data = UIImagePNGRepresentation(image1);
NSString *myGrabbedImage = @"myGrabbedImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];

[data writeToFile:fullPathToFile atomically:YES];

[[self firstImageView]setImage:image1];

[self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerController2:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info2
{
UIImage *image2 = [info2 objectForKey:UIImagePickerControllerOriginalImage];

NSData *data2 = UIImagePNGRepresentation(image2);
NSString *myGrabbedImage2 = @"myGrabbedImage2.png";
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory2 = [path2 objectAtIndex:0];
NSString *fullPathToFile2 = [documentDirectory2 stringByAppendingPathComponent:myGrabbedImage2];

[data2 writeToFile:fullPathToFile2 atomically:YES];

[[self secondImageView]setImage:image2];

[self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerController3:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info3
{
UIImage *image3 = [info3 objectForKey:UIImagePickerControllerOriginalImage];

NSData *data3 = UIImagePNGRepresentation(image3);
NSString *myGrabbedImage3 = @"myGrabbedImage3.png";
NSArray *path3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory3 = [path3 objectAtIndex:0];
NSString *fullPathToFile3 = [documentDirectory3 stringByAppendingPathComponent:myGrabbedImage3];

[data3 writeToFile:fullPathToFile3 atomically:YES];

[[self thirdImageView]setImage:image3];

[self dismissViewControllerAnimated:YES completion:nil];

}
- (void)imagePickerController4:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info4
{
UIImage *image4 = [info4 objectForKey:UIImagePickerControllerOriginalImage];

NSData *data4 = UIImagePNGRepresentation(image4);
NSString *myGrabbedImage4 = @"myGrabbedImage4.png";
NSArray *path4 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory4 = [path4 objectAtIndex:0];
NSString *fullPathToFile4 = [documentDirectory4 stringByAppendingPathComponent:myGrabbedImage4];

[data4 writeToFile:fullPathToFile4 atomically:YES];

[[self fourthImageView]setImage:image4];

[self dismissViewControllerAnimated:YES completion:nil];

}

@end

我应该如何将它应用于所有4个ImageView? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

问题是所有图像选择器都会调用相同的委托方法:

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

为图像选择器控制器设置委托时,如下所示:

[imagePickerController setDelegate:self];

您基本上告诉图像选取器控制器 self 将能够响应图像选择器委托方法。 正如您在UIImagePickerControlDelegate中看到的那样,没有:

– imagePickerController2:didFinishPickingMediaWithInfo:

等等。

首先,我建议像这样重写你的委托方法:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];

    NSData *data = UIImagePNGRepresentation(image1);
    NSString *myGrabbedImage = @"myGrabbedImage.png";
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [path objectAtIndex:0];
    NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];

    [data writeToFile:fullPathToFile atomically:YES];

    if (picker == imagePickerController) {
        [[self firstImageView]setImage:image1];
    } else if (picker == imagePickerController2) {
        [[self secondImageView]setImage:image1];
    } else if (picker == imagePickerController3) {
        [[self thirdImageView]setImage:image1];
    } else {
        [[self fourthImageView]setImage:image1];
    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

这样,所有图像选择器图像都将通过相同的方法处理,从而减少了代码量。