【问题标题】:Using multiple imagepickers on one view在一个视图上使用多个图像选择器
【发布时间】:2013-06-23 02:21:08
【问题描述】:

我有一个工作图像选择器,在按钮单击并按住手势时,允许用户将图像上传到磁盘,并且用户可以使用相同的手势更改图像。但是,唯一的问题是,我需要在同一个视图上执行两次(即我有两个图像视图,两个按钮来更改每个图像视图等),而我不知道如何让第二个工作。这基本上就是视图的样子:

这是我当前的代码:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//
// Saving into Documents folder
//
NSString* path = [NSHomeDirectory() stringByAppendingString:@"/Documents/first.png"];

BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path
                                                  contents:nil attributes:nil];

if (!ok) {
    NSLog(@"Error creating file %@", path);
} else {
    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
    [myFileHandle writeData:UIImagePNGRepresentation(info [UIImagePickerControllerOriginalImage])];
    [myFileHandle closeFile];
}

//
// Loading from documents
//
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
self.chosenImage = loadedImage;
[self.imageView setImage:self.chosenImage];
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onLongTile:(UILongPressGestureRecognizer *)gesture
{
switch ([gesture state]) {
    case UIGestureRecognizerStateBegan:{
        NSString *actionSheetTitle = @"Photo Options"; //Action Sheet Title
        NSString *type = @"Upload Photo";
        NSString *cancelTitle = @"Cancel";
        UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                      initWithTitle:actionSheetTitle
                                      delegate:self
                                      cancelButtonTitle:cancelTitle
                                      destructiveButtonTitle:nil
                                      otherButtonTitles:type, nil];
        [actionSheet showInView:self.view];            }

}
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if  ([buttonTitle isEqualToString:@"Photo"]) {
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:self.imagePicker animated:YES completion:nil];
}
}

用标签更新代码:

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

if (self.imageViewOne.tag == 100)
{
    //
    // Saving into Documents folder
    //
    NSString* path = [NSHomeDirectory() stringByAppendingString:@"/Documents/one.png"];

    BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path
                                                      contents:nil attributes:nil];

    if (!ok) {
        NSLog(@"Error creating file %@", path);
    } else {
        NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
        [myFileHandle writeData:UIImagePNGRepresentation(info [UIImagePickerControllerOriginalImage])];
        [myFileHandle closeFile];
    }

    //
    // Loading from documents
    //
    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
    self.chosenImageOne = loadedImage;
    [self.imageViewOne setImage:self.chosenImageOne];
    [self dismissViewControllerAnimated:YES completion:nil];
}
if (self.imageViewTwo.tag == 200)
{
    //
    // Saving into Documents folder
    //
    NSString* path = [NSHomeDirectory() stringByAppendingString:@"/Documents/two.png"];

    BOOL ok = [[NSFileManager defaultManager] createFileAtPath:path
                                                      contents:nil attributes:nil];

    if (!ok) {
        NSLog(@"Error creating file %@", path);
    } else {
        NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
        [myFileHandle writeData:UIImagePNGRepresentation(info [UIImagePickerControllerOriginalImage])];
        [myFileHandle closeFile];
    }

    //
    // Loading from documents
    //
    NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
    UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
    self.chosenImageTwo = loadedImage;
    [self.imageViewTwo setImage:self.chosenImageTwo];
    [self dismissViewControllerAnimated:YES completion:nil];
}

}

【问题讨论】:

    标签: image uiimagepickercontroller


    【解决方案1】:

    根据我对问题的理解,将标签设置为您的 UIImageView 并进行相应的更改。类似:

    [self.imageViewOne setTag:100];
    [self.imageViewTne setTag:200];
    

    然后在您的代表中,您可以检查标签值并执行必要的操作:

    if ( imageView.tag == 100)
    {
        // Code for first imageView
    }
    

    【讨论】:

    • 抱歉,我对objective-c比较陌生。我将 imageViewOne 和 imageViewTwo 设置为给定的标签,并尝试在 imagePickerController 方法中实现它们。当我对其进行测试时,现在只要我单击任一按钮(即更改 imageViewOne 的按钮和更改 imageViewTwo 的按钮),它就会将相同的图像上传到两个位置。我将更新后的代码添加到我的原始帖子中。任何解决此问题的帮助将不胜感激,谢谢!
    • 你想做什么?你有两个 imageView 对吗?因此,当您单击顶部按钮时,您希望将图像加载到第一个 imageView 中,而当您单击第二个按钮时,您希望将另一个选定的图像加载到第二个 imageView 中?
    • 一种方法是实现两个 UIImagePickerControllers。就像我说的,为每个选择器设置一个标签。当委托被调用时,根据标签分别处理它们。查看此链接了解更多详情:stackoverflow.com/questions/12328859/…
    猜你喜欢
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多