1

私はこの問題に関してここでかなりの数のスレッドを経てきましたが、あまり成功していません.

Web サイトから画像を閲覧しているときに、アクション シートを表示するアイコンをタップ アンド ホールドするか、写真を保存するか、キャンセルします。保存されますが、画像自体ではなく、白い画像が保存されます。私が試した別の方法は、スクリーンショットとしてのみ保存しました(私が求めているものではありません)。

私は特定の画像を求めているのではなく、あらゆる形式のあらゆる画像を求めています。

これは、私が作業している現在のコードのすべてです。

    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);
}
4

3 に答える 3

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];    
}

これが誰かに役立つことを願っています:)

于 2013-08-22T01:33:53.917 に答える