1

I have been through quite a few threads on here regarding this issue, and without to much success.

I'm browsing images from a website, I tap and hold which shows my action sheet, save photo or cancel. It does save, but it saves a white image, not the image itself. Another method I tried only saved as a screen shot (not what I'm after).

I'm not after a specific image, just any image in any format.

This is all of the current code I'm working with.

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

The savePhoto is what I believe is messing up.

Thank you in advanced.

UPDATED: So this says it saves it, but nothing shows up.

-(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);
}
ChrisOSX
  • 724
  • 2
  • 11
  • 28

3 Answers3

3

Decided to circle back to this, as I was taking care of other things in my app. Decided to take a different approach. Instead of messing around trying to discover touches or whatever. I simply just called the save action to download the image to the camera roll. Pretty simple now I can see how it works. If anybody is curious, this is how I did it.

To sum it up, in my webview, I created a controller that will open image extensions. Makes it easier in my opinion to localize it. From there, I added a save button to the nav bar. From that heres what I did.

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

Hope this helps somebody :)

ChrisOSX
  • 724
  • 2
  • 11
  • 28
1

When you request gestureRecognizer.view, you're getting the web view, not what you suppose is an image view. The gesture recognizer returns the view it's connected to, not the view that was tapped.

Also, the correct way to draw the view into the image is to use the views layer:

[_selectedImageView.layer renderInContext:UIGraphicsGetCurrentContext()];

Take a look at this answer: how-to-get-the-image-from-uiwebview-in-ios.

Community
  • 1
  • 1
Wain
  • 118,658
  • 15
  • 128
  • 151
  • This just takes a screen shot, not the actual image itself. Even with implementing that answer in that link – ChrisOSX Jun 28 '13 at 00:35
  • The answer interrogates the web view using javascript to get the image details and then saves that image. – Wain Jun 28 '13 at 06:38
  • Even implemented the link into my code, it says it saves it, but nothing shows up in the simulator's photo app. – ChrisOSX Jul 01 '13 at 02:14
  • Try running it on a device to compare. And give details of what the crash is. – Wain Jul 01 '13 at 05:57
  • With the updated code above it won't crash, but then again it won't show the saved imaged either. – ChrisOSX Jul 01 '13 at 12:11
  • The image isn't saved to disk, or isn't show in the image view, or both? Is the reference to the image view valid when you try to use it? – Wain Jul 01 '13 at 12:14
  • Both, i tried using it on my device, and possibly seeing if it was in the DCIM folder, just not showing in the camera roll view, but no dice. I'm not receiving any errors, so I would assume everything is valid. – ChrisOSX Jul 01 '13 at 13:48
  • If you send messages to `nil` you won't get any errors, it just won't do anything. Debug and check all of your input data, that the image is created and that the image view is set. There could be an access issue when using `dataWithContentsOfURL` but without debugging it's really hard to guess. – Wain Jul 01 '13 at 13:51
  • No idea whats going on, still just saves a white image. I tried using a method already being used in my code to determine file extensions, but that lead no where also. – ChrisOSX Jul 02 '13 at 02:05
0

You should never send drawRect: to a view (except using super in your own drawRect: implementation).

You need to draw the image view's image into the graphics context. Try this:

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

    UIImageWriteToSavedPhotosAlbum(image,
                                   self,
                                   @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),
                                   NULL);
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848