0

所以这一直让我很烦。我想做的是创建一个照片拼贴应用程序,其中有多个图像视图,我将在图像视图上放置按钮,当用户单击按钮时,它会调出相机胶卷,您可以选择一个图像并将其放入那个图像视图。我遇到的问题是,每次用户选择图像时,即使我声明了多个图像,它也只会将其放置在一个图像视图中。图像只是不断被替换。下面是我的代码。

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImagePickerController *imagePicker;
    UIImagePickerController *imagePickerTwo;
}

@property (retain, nonatomic) IBOutlet UISwitch *toggleCamera;
@property (retain, nonatomic) IBOutlet UIImageView *imageViewOne;
@property (retain, nonatomic) IBOutlet UIImageView *imageViewTwo;
- (IBAction)buttonOne:(id)sender;
- (IBAction)buttonTwo:(id)sender;

@end

查看控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize toggleCamera;
@synthesize imageViewOne;
@synthesize imageViewTwo;


- (void)viewDidLoad
{   imagePicker = [[UIImagePickerController alloc] init];
    imagePickerTwo = [[UIImagePickerController alloc] init];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setImageViewOne:nil];
    [self setImageViewTwo:nil];
    [self setToggleCamera:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [imageViewOne release];
    [imageViewTwo release];
    [toggleCamera release];
    [super dealloc];
}


- (IBAction)buttonOne:(id)sender {

    UIImagePickerController *imagePicker;
    imagePicker = [[UIImagePickerController alloc] init];

    if ([self.toggleCamera isOn]) {
        imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
    }else {
        imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    }
    imagePicker.delegate=self;

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self presentModalViewController:imagePicker animated:YES];

    }

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [self dismissModalViewControllerAnimated:YES];
    self.imageViewOne.image=[info objectForKey:
                                    UIImagePickerControllerOriginalImage];
} 


- (IBAction)buttonTwo:(id)sender {
    UIImagePickerController *imagePickerTwo;
    imagePicker = [[UIImagePickerController alloc] init];

    if ([self.toggleCamera isOn]) {
        imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
    }else {
        imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    }
    imagePicker.delegate=self;

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self presentModalViewController:imagePicker animated:YES];

}

-(void)imagePickerController2:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [self dismissModalViewControllerAnimated:YES];
    self.imageViewTwo.image=[info objectForKey:
                             UIImagePickerControllerOriginalImage];
}
@end

建议???请帮助!

4

1 回答 1

0

建议:

制作一个 NSMutableArray ,您可以在其中保留所有 imageView 对象。所以在获取新图像时..您可以在各自的imageView中设置它们,获取它的框架并替换它

于 2013-11-01T11:28:36.057 回答