2010-11-16 98 views
7

我正在創建一個iPad應用程序,該應用程序在水平滾動視圖中顯示多張圖片(UIImageViews)。我想讓用戶在點擊其中一個UIImageView s時能夠將圖像保存到他們的照片庫。我喜歡Safari處理此事的方式:只需點按並按住,直到彈出菜單出現,然後點擊保存圖像。我知道有「UIImageWriteToSavedPhotosAlbum」。但我是iOS開發的新手,我不太確定該怎麼去,以及它放在哪裏(即如何檢測哪個圖像被點擊)。將UIImageView中的圖像保存到iPad照片庫

從我發現的,我看到人們使用UIImage而不是UIImageView。我是否需要將我的視圖轉換爲UIImage?如果是這樣,怎麼樣?如何檢測用戶點擊圖像的時間,以及哪個UIImageView被點擊?如果你能指出我正確的方向,也許我會非常感謝它的一些例子。

回答

32

您可以使用UIImageViewimage屬性來獲取當前圖像:

UIImage* imageToSave = [imageView image]; // alternatively, imageView.image 

// Save it to the camera roll/saved photo album 
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil); 
+0

感謝您的快速回復。但是,如何檢測哪個UIImageView被點擊?是否有一種方法可以在用戶點擊圖像時顯示彈出窗口,以便用戶選擇要保存的按鈕?當「保存」按鈕被按下時,我會把你的陳述放在IBAction中,對吧? – Brian 2010-11-16 19:47:45

+0

您可以將Interface Builder中的UIImageView中的'touch up inside'事件連接到您實現的操作方法(這也可以通過編程方式完成),例如' - (void)touchedImageView:(id)sender',在這種情況下「發件人」將成爲被觸動的觀點。從那裏,你可以展示一個菜單(例如'UIActionSheet')來決定是否保存圖像。 – bosmacs 2010-11-16 20:05:41

+0

我的代碼已關閉,但在界面生成器中沒有看到任何「觸摸」事件。選擇我的一個UIImageView,我進入Inspector窗口,然後是Connections選項卡,對不對?我看到按鈕被選中時的觸摸事件,但不是UIImageView。我應該只是將所有這些UIImageView放入按鈕中嗎? – Brian 2010-11-16 20:29:16

3

在問候你的問題的一部分,詢問如何檢測其的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. 

    } 
} 
4
- (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]; 
} 
1

夫特

// 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. 
      } 
    } 
相關問題