2013-06-27 63 views
1

關於此問題,我在這裏經歷了相當多的線索,並且沒有取得太大的成功。以編程方式將圖像保存到相機膠捲

我正在瀏覽網站上的圖片,我點擊並按住顯示我的操作表,保存照片或取消。它確實可以保存,但它保存了白色圖像,而不是圖像本身。我試過的另一種方法只保存爲屏幕截圖(而不是我之後的)。

我不是在一個特定的圖像後,任何形式的任何圖像。

這是我正在使用的所有當前代碼。

UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; 
    webView.userInteractionEnabled = YES; 
    gestureRecognizer.minimumPressDuration = 0.3; 
    gestureRecognizer.delegate = self; 
    gestureRecognizer.numberOfTouchesRequired = 1; 
    [webView addGestureRecognizer:gestureRecognizer]; 

} 

- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{ 
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ 
     //get the image view that the user selected and save it as your selectedImageView property 
     UIImageView *pressedImageView = (UIImageView *)gestureRecognizer.view; 
     self.selectedImageView = pressedImageView; 

     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil]; 
     actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 
     [actionSheet showInView:self.view]; 
     [actionSheet release]; 

    }} 

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    switch (buttonIndex) { 
     case 0: 

      [self savePhoto]; 

      break; 

     default: 
      break; 

    }} 

-(void)savePhoto{ 

    UIGraphicsBeginImageContext(_selectedImageView.bounds.size); 
    [_selectedImageView drawRect:_selectedImageView.bounds]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageWriteToSavedPhotosAlbum(image, 
            self, 
            @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), 
            NULL); 


} 

- (void) savedPhotoImage:(UIImage *)image 
    didFinishSavingWithError:(NSError *)error 
       contextInfo:(void *)contextInfo 
{ 
    NSString *message = @"This image has been saved to your Photos album"; 
    if (error) { 
     message = [error localizedDescription]; 
    } 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                message:message 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 

savePhoto是我相信搞亂了。

謝謝您的高級。

已更新: 所以這說,它保存它,但沒有出現。

-(void)savePhoto { 

    NSLog(@"TAPPED"); 
    //Touch gestures below top bar should not make the page turn. 
    //EDITED Check for only Tap here instead. 
     CGPoint touchPoint = [touch locationInView:self.view]; 

     NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; 
     bool pageFlag = [userDefaults boolForKey:@"pageDirectionRTLFlag"]; 
     NSLog(@"pageFlag tapbtnRight %d", pageFlag); 

      NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y]; 
      NSString *urlToSave = [webView stringByEvaluatingJavaScriptFromString:imgURL]; 
      NSLog(@"urlToSave :%@",urlToSave); 
      NSURL * imageURL = [NSURL URLWithString:urlToSave]; 
      NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
      UIImage * image = [UIImage imageWithData:imageData]; 
      imageView.image = image;//imgView is the reference of UIImageView 

      UIImageWriteToSavedPhotosAlbum(image, 
              self, 
              @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), 
              NULL); 
} 

回答

3

決定回到這個,因爲我正在照顧我的應用程序中的其他東西。決定採取不同的方法。而不是亂搞嘗試發現觸摸或其他。我只是簡單地稱保存操作將圖像下載到相機膠捲。現在很簡單,我可以看到它是如何工作的。如果有人好奇,我就是這麼做的。

總結一下,在我的webview中,我創建了一個打開圖像擴展的控制器。使我更容易將其本地化。從那裏,我添加了一個保存按鈕到導航欄。從那裏繼承我所做的。

-(void)savePhoto { 
    NSURL *imageURL = receivedURL; 
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]]; 
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil); 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
} 

- (void) savedPhotoImage:(UIImage *)image 
    didFinishSavingWithError:(NSError *)error 
       contextInfo:(void *)contextInfo 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
                message:@"This image has been saved to your camera roll" 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    [alert show]; 
    [alert release];  
} 

希望這有助於有人:)

0

你不應該發送drawRect:到一個視圖(除了在自己的drawRect:實現使用super)。

您需要將圖像視圖的圖像繪製到圖形上下文中。試試這個:

- (void)savePhoto { 
    UIGraphicsBeginImageContext(_selectedImageView.bounds.size); 
    [_selectedImageView.image drawInRect:_selectedImageView.bounds]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageWriteToSavedPhotosAlbum(image, 
            self, 
            @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), 
            NULL); 
} 
+0

這只是拋出一個錯誤,使模擬器崩潰。 – ChrisOSX

1

當你請求gestureRecognizer.view,你得到的Web視圖,而不是你想是的形象圖。手勢識別器返回它連接的視圖,而不是被點擊的視圖。

此外,繪製視圖到圖像的正確方法是使用視圖層:

[_selectedImageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

看看這個答案:how-to-get-the-image-from-uiwebview-in-ios

+0

這隻需要一個屏幕截圖,而不是實際的圖像本身。即使在該鏈接中實現該答案 – ChrisOSX

+0

答案會詢問使用javascript獲取圖像細節的Web視圖,然後保存該圖像。 – Wain

+0

即使在我的代碼中實現了鏈接,它說它可以保存它,但模擬器的照片應用程序中沒有任何內容顯示。 – ChrisOSX

相關問題