【问题标题】:Importing multiple images from Photo Library to App将照片库中的多张图片导入到 App
【发布时间】:2011-06-20 00:24:07
【问题描述】:

我正在尝试创建一个应用程序,让您可以从照片库中导入照片并将它们插入指定的 UIImageViews。我能够让它在我的界面中为其中一个 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];
}

【问题讨论】:

    标签: iphone ios xcode xcode4 uiimagepickercontroller


    【解决方案1】:

    您在委托方法imagePickerController:didFinishPickingImage:editingInfo: 中的imageView 没有改变。您可能应该有一个用于当前图像视图的实例变量,比如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 {
            [..]
        }
    }
    

    但是,您必须对每种方法都执行此操作。我不确定此方法的确切触发器是什么,但在操作方法中包含 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,当按下其各自的按钮时,它们应该由从照片库中选择的图片填充。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 2016-07-13
    • 2015-03-08
    相关资源
    最近更新 更多