How to align an image in the center in WKWebView objective c - ios

Here is my code to display image.
NSString *doc = #"http://13.232.1.87:3000/images/flights/bc4952c8ec51588cdbd72d91c4e1533562273837.jpeg";
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:doc]];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];

I see your link points to a simple jpg image.
You might then feed the WKWebView with html code instead of the direct link to the image, using
[webView loadHTMLString:#"<html>...</html>" baseURL:nil];
[edit] Or, if the link is always pointing to an image, you can consider not even using the WKWebView replacing it with a UIImage:
NSURL *url = [NSURL URLWithString:doc];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
or, for shortness:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:doc]]];
And then set the image frame (or constraints) like you would with the WKWebView

Related

NSData to uiimage return null

_pageImages is nsmutableArry I'm store the image url.
_pageImages = [[NSMutableArray alloc]initWithObjects:#"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com",#"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com", nil];
Want to display this url images in uiimageview.
NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *tmpImage = [UIImage imageWithData:data];
_backgroundImageView.frame = CGRectMake(0 ,10, 320, 320);
_backgroundImageView.image = tmpImage;
[self.view addSubview:_backgroundImageView];
I saw many examples but i can't get the proper solution. new for development. help me..
The problem is in the URL!
Try to access your images URL, all I got is "Unauthorized".
Run this sample code, your code, just using a different image address.
Check for the correct image address, and it should run as expected.
NSMutableArray *pageImages = [[NSMutableArray alloc]initWithObjects:#"http://findicons.com/files/icons/1636/file_icons_vs_3/128/url.png",#"http://findicons.com/files/icons/1636/file_icons_vs_3/128/pdf.png", nil];
NSURL *url = [NSURL URLWithString:[pageImages objectAtIndex:0]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *tmpImage = [UIImage imageWithData:data];
_depImage.frame = CGRectMake(0 ,10, 320, 320);
_depImage.image = tmpImage;
try this by sending AsynchronousRequest for image downloading--
NSURL *url = [NSURL URLWithString:[_pageImages objectAtIndex:i]];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
_backgroundImageView.image = [UIImage imageWithData:data];
}];
We can simply use following to Load Image data from URL
_backgroundImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLwithString:urlString]]];
No need of Calling a service call here.
Hope it helps..

Create an UIImage out of a website's URL

NSURL *imgURL = [NSURL URLWithString:#"stackoverflow.com"];
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
UIImage *img = [[UIImage alloc] initWithData:imgData];
Using with these codes, is it possible to create an UIImage out of a website's URL? Sorry if the question is not clear, what I mean is to screenshot the whole webpage to produce an UIImage. Can I do that with these?
From this link. yes u can.
NSURL *imgURL = [NSURL URLWithString:#"http://www.gettyimages.in/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg
"];
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
UIImage *img = [[UIImage alloc] initWithData:imgData];
If u need to load images without freezing the app, and also to implement thumbnail and cache,
Try SDWebImage framework
https://github.com/rs/SDWebImage
Sample Code
[imageView sd_setImageWithURL:[NSURL URLWithString:#"Get each url from array and set it here using indexpath.row"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Screenshot
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImageJPEGRepresentation(imageView, 1.0 ); //you can use PNG too

Image doesn't fit web view

Am displaying a Image on a web view and it doesn't fit the web view completely and shows blank white space around the border.How can i fit or scale the image completely to the size of web view?
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.webView.backgroundColor=[UIColor blackColor];
self.webView.delegate = self;
[self.webView setScalesPageToFit:YES];
Try this html code snippet
<html><body><img src='%#' width='100%' height='100%'></body></html>
It will fill the web view completely without any white space.
try to load like this
- (void) showImageWithLoadHTMLString {
// Create URL string for image file location
NSURL *imageURL = [NSURL fileURLWithPath: path];
// Create HTML string from image URL
NSString *htmlString = #"<html><body><img src='%#' width='900'></body></html>";
NSString *imageHTML = [[NSString alloc] initWithFormat:htmlString, imageURL];
// Load image in UIWebView
self.webView.backgroundColor=[UIColor blackColor];
self.webView.scalesPageToFit = YES;
[self.webView loadHTMLString:imageHTML baseURL:nil];
}

ImageView loads local image but not from URL

I am having trouble getting an image to load from a URL. I have CollectionViewCell subclass with an image view setup like this:
#property (strong, nonatomic) IBOutlet UIImageView *albumCover;
I cannot get it to load images from a URL. Here is the code im using:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
cell.albumCover = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
Meanwhile, this works:
cell.albumCover.image = [UIImage imageNamed:#"lock.png"];
I know the URL connection is getting the proper image back because I am monitoring it with Fiddler. Why isn't the image showing up then?
I was allocating the UIImageView object again instead of just initializing it with the image. The correct code is:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
[cell.albumCover initWithImage:[UIImage imageWithData: imageData]];
I use SDWebImageView:
Asynchronous image downloader with cache support with an UIImageView category
http://hackemist.com/SDWebImage/doc
You should use fileURLWithPath: instead of the URLWithString:.
URLWithString: is for WWW URL.
And an example:
NSString *rootPath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [rootPath stringByAppendingPathComponent:#"example.mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
yourMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];

UITableViewCell images not loading ios

I am trying to download images asynchronously and display them in UITableViewCell imageView. The problem is that my NSURL is nil for some reason..
NSString *urlString = feed[#"imageURL"];
NSLog(#"urlString %#", urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"url image %#", url);
This will print something like
urlString http://www.....image.jpg
url image (null)
Can someone please tell me why this is happening? Thanks.
What your doing is getting the contents within a url with NSURL. Once you have the contents of the url, you are basically saying to the compiler, load these SOMETHING contents from this url. But that SOMETHING whether it's an image or json object or whatever, needs to be loaded into an NSData object then load the NSData object into a UIImage object. Now you can just say cell.imageView.image = theUIimage for your exact problem but heres a simple example that just loads such UIImage into a UIImageView like so:
NSURL *URL = [NSURL URLWithString:#"http://l.yimg.com/a/i/us/we/52/37.gif"];
NSData *data = [NSData dataWithContentsOfURL:URL];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageView.image = img;
[self.view addSubview:imageView];
Please no downvote :)

Resources