Iphone 将多个图像从照片库导入应用程序

Iphone 将多个图像从照片库导入应用程序,iphone,ios,xcode,xcode4,uiimagepickercontroller,Iphone,Ios,Xcode,Xcode4,Uiimagepickercontroller,我正在尝试创建一个应用程序,允许您从照片库导入照片并将其插入指定的UIImageView。我能够让它在我的界面中的一个UIImageView中工作,但无法将任何内容分配给第二个UIImageView 如果有人能告诉我我做错了什么,我将不胜感激!谢谢SBB 这是我的密码: @implementation FlipBook2ViewController - (IBAction)selectExistingPicture { if ([UIImagePickerController isS

我正在尝试创建一个应用程序,允许您从照片库导入照片并将其插入指定的UIImageView。我能够让它在我的界面中的一个UIImageView中工作,但无法将任何内容分配给第二个UIImageView

如果有人能告诉我我做错了什么,我将不胜感激!谢谢SBB

这是我的密码:

@implementation FlipBook2ViewController


- (IBAction)selectExistingPicture {
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker =
        [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsImageEditing = NO;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Error accessing photo library" 
                              message:@"Device does not support a photo library" 
                              delegate:nil 
                              cancelButtonTitle:@"Dismiss" 
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}


- (IBAction)selectExistingPicture2 {
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *picker =
        [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsImageEditing = NO;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Error accessing photo library" 
                              message:@"Device does not support a photo library" 
                              delegate:nil 
                              cancelButtonTitle:@"Dismiss" 
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}


#pragma mark  -
- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo {
    imageView.image = image;
    [picker dismissModalViewControllerAnimated:YES];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissModalViewControllerAnimated:YES];
}

代理方法中的
imagePickerController:didFinishPickingImage:editingInfo:
中的
imageView
没有更改。您可能应该为当前图像视图提供一个实例变量,比如说
currentImageView
,它们实现委托方法,比如

- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo {
    currentImageView.image = image;
    [picker dismissModalViewControllerAnimated:YES];

}
但是,要使其正常工作,您必须稍微更改
选择ExistingPicture

- (IBAction)selectExistingPicture {
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) {

        currentImageView = imageView1; // Set it to the image view that will get changed

        [..]
    }
    else {
        [..]
    }
}
但是,对于每种方法,您都必须这样做。我不确定此方法的确切触发器是什么,但在action方法中使用
sender
参数是合适的,以避免重复

- (IBAction)selectExistingPicture:(id)sender {
    switch(sender.tag) {
        /* set `currentImageView` based on the tag */
    }

    /* Rest of your method from the question remains the same */
}

谢谢你的建议!我来试试看!隐马尔可夫模型。。似乎不起作用。我有两个按钮和两个ImageView,当按下相应的按钮时,它们应该由从照片库中选择的图片填充。