将多个图像从照片库导入到App

时间:2011-06-20 00:24:07

标签: iphone ios xcode xcode4 uiimagepickercontroller

我正在尝试创建一个应用程序,让您从照片库导入照片并将其插入指定的UIImageViews。我能够在我的界面中为其中一个UIImageViews工作,但无法分配给第二个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];
}

1 个答案:

答案 0 :(得分:0)

委托方法imageView中的imagePickerController:didFinishPickingImage:editingInfo:未发生变化。您可能应该为当前图像视图设置一个实例变量,比如像currentImageView这样的实例变量,它们实现委托方法,如,

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

}

为了使其正常工作,您必须稍微更改selectExistingPicture

- (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 */
}