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);
}
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 回答
1

当您请求时gestureRecognizer.view,您将获得网络视图,而不是您认为的图像视图。手势识别器返回它连接的视图,而不是被点击的视图。

此外,将视图绘制到图像中的正确方法是使用视图层:

[_selectedImageView.layer renderInContext:UIGraphicsGetCurrentContext()];

看看这个答案:how-to-get-the-image-from-uiwebview-in-ios

于 2013-06-27T23:43:25.613 回答
0

您永远不应该发送drawRect:到视图(除非super在您自己的drawRect:实现中使用)。

您需要将图像视图的图像绘制到图形上下文中。尝试这个:

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

    UIImageWriteToSavedPhotosAlbum(image,
                                   self,
                                   @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),
                                   NULL);
}
于 2013-06-27T23:42:44.877 回答