【问题标题】:Save image in UIImageView to iPad Photos Library将 UIImageView 中的图像保存到 iPad 照片库
【发布时间】:2011-05-11 00:43:51
【问题描述】:

我正在创建一个 iPad 应用程序,它在水平滚动视图中有几张图片 (UIImageViews)。我想让用户在点击UIImageViews 之一时能够将图像保存到他们的照片库中。我喜欢 Safari 处理此问题的方式:您只需点击并按住直到出现弹出菜单,然后单击保存图像。我知道有“UIImageWriteToSavedPhotosAlbum”。但我是 iOS 开发的新手,我不太确定该去哪里以及把它放在哪里(即如何检测哪个图像被点击)。

根据我的发现,我看到人们使用UIImage 而不是UIImageView。我是否需要将我的视图转换为UIImage?如果是这样,怎么做?如何检测用户何时点击图像,以及点击了哪个UIImageView?如果你能指出我正确的方向,也许还有一些例子,我将不胜感激。

【问题讨论】:

    标签: iphone xcode ipad uiimageview photolibrary


    【解决方案1】:

    您可以使用UIImageViewimage 属性来获取当前图像:

    UIImage* imageToSave = [imageView image]; // alternatively, imageView.image
    
    // Save it to the camera roll / saved photo album
    UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
    

    【讨论】:

    • 感谢您的快速回复。但是我如何检测哪个 UIImageView 被点击了呢?当用户点击允许用户选择要保存的按钮的图像时,有没有办法让弹出窗口出现?当“保存”按钮被按下时,我会把你的声明放在那个 IBAction 中,对吧?
    • 您可以将 Interface Builder 中 UIImageView 上的“touch up inside”事件连接到您实现的操作方法(这也可以通过编程方式完成),例如-(void)touchedImageView:(id)sender,在这种情况下为@987654326 @ 将是被触摸的视图。从那里,您可以显示一个菜单(例如UIActionSheet)来决定是否保存图像。
    • 我的代码已关闭,但在 Interface Builder 中没有看到任何“触摸”事件。选择我的 UIImageViews 之一,我转到 Inspector 窗口,然后是 Connections 选项卡,对吗?选择按钮但不是UIImageView时,我会看到触摸事件。我应该把所有这些 UIImageViews 都变成按钮吗?
    • 抱歉,忘记了。你可以试试这个问题中的建议:stackoverflow.com/questions/855095/…——要么继承 UIImageView 以覆盖 touch* 方法,要么在每个 UIImageView 上放置一个隐藏按钮,并将触摸事件连接到按钮。
    • 我昨天玩了一点,但我的问题是这是在滚动视图中。因此,如果用户首先触摸按钮然后移动滚动,则它不会滚动。我用谷歌搜索了一下这个问题,但我还没有找到任何有效的方法。不过仍在努力。感谢您的帮助。
    【解决方案2】:

    关于您的问题中询问如何检测哪个 UIImageView 被点击的部分,您可以使用如下代码:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
     [super touchesEnded:touches withEvent:event];
     UITouch *touch = [touches anyObject];
     CGPoint touchEndpoint = [touch locationInView:self.view]; 
     CGPoint imageEndpoint = [touch locationInView:imageview];
     if(CGRectContainsPoint([imageview frame], touchEndpoint))
     {
     do here any thing after touch the event.
    
      }
    }
    

    【讨论】:

      【解决方案3】:
      - (IBAction)TakePicture:(id)sender {
      
      
          // Create image picker controller
          UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
      
          // Set source to the camera
          imagePicker.sourceType =  UIImagePickerControllerSourceTypeCamera;
      
      
          // Delegate is self
          imagePicker.delegate = self;
      
      
          OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
      
         // imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
      
          // Insert the overlay:
          imagePicker.cameraOverlayView = overlay;
      
         // Allow editing of image ?
          imagePicker.allowsImageEditing = YES;
          [imagePicker setCameraDevice:
           UIImagePickerControllerCameraDeviceFront];
          [imagePicker setAllowsEditing:YES];
          imagePicker.showsCameraControls=YES;
          imagePicker.navigationBarHidden=YES;
          imagePicker.toolbarHidden=YES;
          imagePicker.wantsFullScreenLayout=YES;
      
      
          self.library = [[ALAssetsLibrary alloc] init];
      
      
          // Show image picker
          [self presentModalViewController:imagePicker animated:YES];
      }
      
      
      
      
      
      - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
      {
          // Access the uncropped image from info dictionary
          UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
      
      
      
      
          // Save image to album
          UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
      
      
          // save image to custom album
          [self.library saveImage:image toAlbum:@"custom name" withCompletionBlock:^(NSError *error) {
              if (error!=nil) {
                  NSLog(@"Big error: %@", [error description]);
              }
          }];
      
          [picker dismissModalViewControllerAnimated:NO];
      
      
      }
      
      
      
      - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
      {
          UIAlertView *alert;
      
          // Unable to save the image  
          if (error)
              alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                 message:@"Unable to save image to Photo Album." 
                                                delegate:self cancelButtonTitle:@"Ok" 
                                       otherButtonTitles:nil];
          else // All is well
              alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                                 message:@"Image saved to Photo Album." 
                                                delegate:self cancelButtonTitle:@"Ok" 
                                       otherButtonTitles:nil];
      
      
          [alert show];
      }
      
      
      
      - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
      {
          // After saving iamge, dismiss camera
          [self dismissModalViewControllerAnimated:YES];
      }
      

      【讨论】:

        【解决方案4】:

        Swift 中:

            // Save it to the camera roll / saved photo album
            // UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, nil, nil, nil) or 
            UIImageWriteToSavedPhotosAlbum(self.myUIImageView.image, self, "image:didFinishSavingWithError:contextInfo:", nil)
        
            func image(image: UIImage!, didFinishSavingWithError error: NSError!, contextInfo: AnyObject!) {
                    if (error != nil) {
                        // Something wrong happened.
                    } else {
                        // Everything is alright.
                    }
            }
        

        【讨论】:

          猜你喜欢
          • 2020-11-28
          • 2015-10-20
          • 1970-01-01
          • 1970-01-01
          • 2013-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-13
          相关资源
          最近更新 更多