【问题标题】:UIAlertView not showing error when photo fails to save照片保存失败时 UIAlertView 不显示错误
【发布时间】:2012-11-26 17:43:07
【问题描述】:

我又在苦苦挣扎,这一次得到一个警报视图以显示正确的错误信息,我有一个 2 指点击手势,当您单击例如带有 2 个手指的谷歌图像(全尺寸)图像时,它会保存图像并显示正确的警报“图像已保存”,但您可以用 2 根手指单击任意位置,它会显示“图像已保存”,即使它没有,因为没有要保存的图像。因此,如果有人可以帮助我在他们对图像以外的其他东西做手势时显示一条消息 这是问题可能在其他地方的完整代码,因为 *tagname 变量也未使用 提前致谢

    - (void)webViewDidFinishLoad:(UIWebView *)webView
{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

//DoubleTap Gesture
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTouchesRequired = 2;
[self.myWebView addGestureRecognizer:doubleTap];


}
//Double Tap Method
-(void) doubleTap :(UITapGestureRecognizer*) sender {
int scrollPositionY = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
int scrollPositionX = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.pageXOffset"] intValue];

int displayWidth = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"window.outerWidth"] intValue];
CGFloat scale = myWebView.frame.size.width / displayWidth;

CGPoint pt = [sender locationInView:self.myWebView];
pt.x *= scale;
pt.y *= scale;
pt.x += scrollPositionX;
pt.y += scrollPositionY;

NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).tagName", pt.x, pt.y];
NSString * tagName = [self.myWebView stringByEvaluatingJavaScriptFromString:js];

NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", startPoint.x, startPoint.y];
NSString *urlToSave = [self.myWebView stringByEvaluatingJavaScriptFromString:imgURL];


NSURL *url = [NSURL URLWithString:urlToSave];

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
UIImageWriteToSavedPhotosAlbum(image,self,@selector(savedPhotoImage:didFinishSavingWithError:contextInfo:),nil);
}
//Alert for double tap save photo
- (void)   savedPhotoImage:(UIImage *)image
  didFinishSavingWithError:(NSError *)error
           contextInfo:(void *)contextInfo
{
UIAlertView *alert;

// Unable to save the image
if (error)
    alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                       message:@"Unable to save image to Photo Album."
                                      delegate:self cancelButtonTitle:@"Ok"
                             otherButtonTitles:nil];
else // All is well
    alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                       message:@"Image saved to Photo Album."
                                      delegate:self cancelButtonTitle:@"Ok"
                             otherButtonTitles:nil];
[alert show];
}    

【问题讨论】:

  • 所以等等,当图像不存在时,或者当用户点击图像以外的任何东西时,你想显示错误吗?
  • 在 else 之后,放一个 { 并在 [alert show] 之后放一个结束; }。 [alert show] 现在一直在执行
  • 请显示您添加点击手势识别器的代码以及您如何使用它,我认为问题就在那里。
  • 是否在主线程以外的线程上调用了 savedPhotoImage?如果是这样,这将是警报未能显示的原因
  • 好的,所以在查看您的代码后,可能会发生以下两种情况之一,图像可能为零,因为要保存的 url 没有返回有效的图像 url,或者因为您正在下载图像互联网,调用writeToSavedPhotoAlbum时图像数据尚未完成。在 writeToSavedPhotoAlbum 行上设置一个断点,然后检查该点保存的图像和 url。

标签: objective-c ios error-handling uiimage uialertview


【解决方案1】:
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                   message:@"Unable to save image to Photo Album."
                                  delegate:self cancelButtonTitle:@"Ok"
                         otherButtonTitles:nil];
else // All is well.
   //Without a curly brace here, only one line will get executed. If you want [alert show] to get executed, you have to put a curly brace above and right after wards
  {
    alert = [[UIAlertView alloc] initWithTitle:@"Success"
                                   message:@"Image saved to Photo Album."
                                  delegate:self cancelButtonTitle:@"Ok"
                         otherButtonTitles:nil];
    [alert show];
 }
}  

【讨论】:

  • 我相信原始代码的意图是在 if/else 块中进行创建,并显示为两种情况的通用代码。
  • 是的,它仍然在给出带有括号的消息,是的,savedPhoto 图像只出现在上面的代码中,没有其他地方出现。为了进行测试,我将该方法移到了操作表按钮中,并且相同的是,它仅在图像是屏幕上唯一的内容时才保存图像,例如当您单击在谷歌图像中查看全尺寸图像时,当您在任何地方尝试时,它都会保存它否则它不会保存任何东西,但警报说成功,任何想法,伙计们
  • 当我保存图像时,* tagName 变量也未使用,但老实说我有点超出我的深度
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
相关资源
最近更新 更多