关于这个问题,我在这里经历了不少线程,但没有取得多大成功。
我正在浏览网站上的图片,点按并显示我的操作表,保存照片或取消。它确实保存,但它保存了白色图像,而不是图像本身。我试过的另一种方法只保存为屏幕截图(不是我想要的)。
我不是在拍摄特定图像,而是拍摄任何格式的图像。
这是我正在使用的所有当前代码。
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);
}
答案 0 :(得分: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];
}
希望这有助于某人:)
答案 1 :(得分:1)
当您请求gestureRecognizer.view
时,您将获得网络视图,而不是您认为的图像视图。手势识别器返回它所连接的视图,而不是被轻击的视图。
此外,将视图绘制到图像中的正确方法是使用视图图层:
[_selectedImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
答案 2 :(得分: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);
}