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