how to add a image from the photo storage to a UIW

2020-08-01 07:07发布

问题:

Is there anyway to display a image in a UIWebview from the iphone photo storage

回答1:

May be this is the suitable code in the upload image,

- (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:

in your image picker delegate, save the file locally, basic code snippet, not complete

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

the in your webview, set the base path of the HTML page you are going to load to the same path used for saving the image file.

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


回答3:

If you just wanted to display the image, the code below works just fine (using the UIImagePickerDelegate)

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

hope this helps.



回答4:

Yes, you first must use the UIImagePickerController. It will cause a new window from the iPhone to appear and the user will be requested to pick a photo.

When they pick the photo, you can get access to this photo, you can either dismiss the picker or the user can continue picking photos.

You are not allowed direct access to users photos and wont be able to access their photos with asking the user first. This makes total sense, as a user I would not want developers sneaking into my photo library and uploading my photos in the background.

To add these to a UIWebView, you will nee to set the [UIWebView basePath] to your (document directory where you saved the photo from the above process), I have got this working, search stackoverflow for UIWebView Basepath, or check documentation.

Cheers, John.