【问题标题】:how to add a image from the photo storage to a UIWebview如何将照片存储中的图像添加到 UIWebview
【发布时间】:2010-09-06 22:53:55
【问题描述】:

无论如何要在 iphone 照片存储中的 UIWebview 中显示图像

【问题讨论】:

    标签: iphone uiwebview


    【解决方案1】:

    可能这是上传图片中合适的代码,

    - (BOOL)webView:(UIWebView*)webViewRef shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    
        //we are listening out for links being click
        if (navigationType == UIWebViewNavigationTypeLinkClicked) 
        {
    
            NSURL *URL = [request URL]; //Get the URL
    
    
            if ( [[URL scheme] isEqualToString:@"objc"] ) {
    
    
                webView = webViewRef;
    
    
                SEL method = NSSelectorFromString( [URL host] );
    
                if ([self respondsToSelector:method])
                {
    
                    [self performSelector:method withObject:nil afterDelay:0.1f];
                }
    
                return NO;
            }
    
        }
    
       return YES;
    }
    -(void)takePicture
    {
    
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
    
    
        id delegate = [[UIApplication sharedApplication] delegate];
        [[delegate viewController] presentModalViewController:imagePicker animated:YES];
    
        [imagePicker release];
    
    }
    
    #pragma mark - Image Picker
    
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    
        UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    
        NSData *flatImage = UIImageJPEGRepresentation(image, 0.1f);
    
    
    
        NSString *image64 = [flatImage base64EncodedString];
    
    
        NSString *js = [NSString stringWithFormat: @"processImage('data:image/jpeg;base64,%@')", image64];
        [webView stringByEvaluatingJavaScriptFromString:js];
    
    
        [picker dismissModalViewControllerAnimated:YES];
    
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    
        [picker dismissModalViewControllerAnimated:YES];
    }
    

    【讨论】:

      【解决方案2】:

      在你的图像选择器委托中,将文件保存到本地,基本代码sn-p,不完整

      - (void)imagePickerController:(UIImagePickerController *)picker 
                                         didFinishPickingMediaWithInfo:(NSDictionary *)info   {
      
              NSString *path = [[NSBundle mainBundle] bundlePath];
              NSData *data = UIImageJPEGRepresentation( [info objectForKey:@"UIImagePickerControllerOriginalImage"] , 1.0);
              NSString *fullPath = [path stringByAppendingPathComponent:@"image_tmp.jpg"];
              if ([[NSFileManager defaultManager] createFileAtPath:fullPath contents:data attributes:nil] ) {
                          NSLog([NSString stringWithFormat:@"file successfully written to path %@",fullPath],nil);
              }
       }
      

      在您的网页视图中,将您要加载的 HTML 页面的基本路径设置为用于保存图像文件的相同路径。

      NSString *path = [[NSBundle mainBundle] bundlePath];
      NSURL *baseURL = [NSURL fileURLWithPath:path];
      [webView loadHTMLString:htmlString baseURL:baseURL];
      

      【讨论】:

        【解决方案3】:

        如果你只是想显示图像,下面的代码可以正常工作(使用 UIImagePickerDelegate)

        - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
            [_youWebView loadData:UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]) MIMEType:@"image/jpeg" textEncodingName:@"utf-8" baseURL:nil];
        }
        

        希望这会有所帮助。

        【讨论】:

          【解决方案4】:

          是的,您首先必须使用 UIImagePickerController。这将导致 iPhone 出现一个新窗口,并要求用户选择一张照片。

          当他们选择照片时,您可以访问该照片,您可以关闭选择器或用户可以继续选择照片。

          您不能直接访问用户的照片,也不能通过先询问用户来访问他们的照片。这完全有道理,作为用户,我不希望开发人员潜入我的照片库并在后台上传我的照片。

          要将这些添加到 UIWebView,您需要将 [UIWebView basePath] 设置为您的(您在上述过程中保存照片的文档目录),我已经完成了这项工作,在 stackoverflow 中搜索 UIWebView Basepath,或检查文档。

          干杯,约翰。

          【讨论】:

          • 你有这个方法的工作示例吗?我可以看看。
          • 在 uiwebview basepath 上找不到任何东西。你有一个工作示例我可以看看。
          猜你喜欢
          • 2014-01-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          • 2012-05-06
          • 1970-01-01
          • 2014-04-29
          • 2015-10-20
          相关资源
          最近更新 更多