如何将图片从照片存储添加到UIWebview

时间:2010-09-06 22:53:55

标签: iphone uiwebview

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

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

在您的图片选择器委托中,在本地保存文件,基本代码段,不完整

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

答案 2 :(得分:1)

如果你只是想显示图像,下面的代码就可以了(使用UIImagePickerDelegate)

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

希望这会有所帮助。

答案 3 :(得分:-1)

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

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

您不能直接访问用户的照片,也不能在首先询问用户的情况下访问他们的照片。这是完全合理的,作为一个用户,我不希望开发人员潜入我的照片库并在后台上传我的照片。

要将这些添加到UIWebView,您需要将[UIWebView basePath]设置为您(从上面的过程保存照片的文档目录),我有这个工作,搜索UIWebView Basepath的stackoverflow,或检查文档。

干杯,约翰。