I'm trying to pass the user on to the interface based on their cell selection in the UITableView, ie, if the user selects cell 1, they are taken to view model 2, containing UIWebView. UIWebView then displays local file, cell1.html.
Currently, I've manage to get placeholder using:
selectedCellText.text = selectedCell;
to display the name of the cell selected. How do I get it to directly pass to the UIWebView, stick UIWebView in the interface and link it using:
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:frame];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *stringUrl = [mainBundle pathForResource:#"selectedCell" ofType:#"html"];
NSURL *baseUrl = [NSURL fileURLWithPath:stringUrl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:baseUrl];
[myWebView loadRequest:urlRequest];
My other issue is that some of the cell names have spaces in them, and for simplicity, I'd like to ensure that there are no spaces (actually, will it even work with spaces in the name, I assume with %20 ?
Thanks
Fixed by flowing table -> html page
Related
(This looks like a question that has been asked/answered 100 times but I can't get it to work.)
I have a view controller that is story board based. Nested in the main view is a webUIView.
In the controller method viewDidLoad I am simply specifying the URL and setting the view and the webUIView to autoresize:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"web_app_build"]];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_browser loadRequest:request];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.autoresizesSubviews = TRUE;
self.browser.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
I also added this method
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.browser.frame = self.view.bounds;
}
When I rotate the device the webUIView rotates, but doesn't resize to fill the screen. I did try writing my own rotation handler but this seems the entirely wrong way to do that.
I did read the apple doc on the topic. How can I make the webUIview resize to fit the display size on rotate?
The issue you're having is that self.view.bounds has most likely not been adjusted yet to the bounds after rotation. Set a breakpoint to check this out. I would move the code to viewDidLayoutSubviews: instead. If you don't need to support versions of iOS before iOS 6, I'd strongly recommend looking into using Auto Layout for your view objects. It will make your life a lot easier.
I need to load image from server(url) to UITableView. So i tired below code, but no use.
UIImageView *img=[[UIImageView alloc]init];
img.frame=fr;
img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:countimg]]];
[cell.contentView addSubview:img];
Here countimg is NSMutableArray. countimg contains url of all images to load. But its not working, because its not a string variable. I don't know how to change NSMutuableArray to String.
Anyone help me.
You can get string urls from countimg array like this,
[countimg objectAtIndex:index];
Where index is an integer of range between >=0 && < [countimg count]
Now if you're using countimg array in UITableView delegate then, you've replace index variable with indexPath.row.
So for now your code would look like this,
UIImageView *img=[[UIImageView alloc]init];
img.frame=fr;
img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[countimg objectAtIndex:indexPath.row]]]]; //note this
[cell.contentView addSubview:img];
Update:
As #DavidAtkinson, suggestion, you should not use dataWithContentsOfURL method to load a NSURL to download a image. As its perform in main thread and will stuck the UI until download would not completes. Its better to load image in background.
Also, there's some asynchronous UIImageView's available, one which I am using is SDWebImage.
Use SDWebImage to load your image from server. here using this library you can put placeholder image in tableview cell's image view till the image loaded. You can even cache your image by just doing a single line code
[UIImageView setImageWithUrl:[pass your URL here] placeholderImage:[pass name of placeholder image] options:SDWebImageRefreshCached]
If you are trying to load your images from URL into a tableview,the SDWebImage code from Github does not specify how to do that.You have to create an object at index,then load the object for key name from the file you are parsing from.This will require you to change the SDWebImage code so that instead of parsing directly from a specific url,its parsing from
an object key name that contains an image url from your json or xml file.
Here is a sample code for loading an image from an object key in a json:
NSURL* url = [NSURL URLWithString:[dictionaryObject valueForKey:#"yourfile"]];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImageView *imgViewThumb=[[UIImageView alloc]initWithFrame:imgView.frame];
[imgViewThumb setImage:[UIImage imageWithData:data]];
[cell addSubview:imgViewThumb];
I have an app that only supports portrait mode and a local PDF file in landscape orientation that I am loading into a fullscreen UIWebView. When I open the view, the pdf is displayed to full width, but I would like to display it full height (so that actually only a part of it is visible initially).
this is what I currently have:
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"plan" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[mapView loadRequest:request];
}
scalesPageToFit is set to YES in Storyboard, as I would like users to be able to change the zoom level after initially loading the pdf.
contentMode is set to scaleToFill (in Storyboard, too)
I've tried to play around with these values and also with setting mapView.scrollView.zoomScale programmatically, but no success so far.
How can I achieve that the PDF is loaded into the UIWebView full height instead of full width?
Currently, I'm working on the client for the website. I have tonne of images that I need to load to my TableView. Here is what I'm currectly doing:
NSDictionary *propertyItems = [self.items objectAtIndex:indexPath.row];
cell.fio.font = [UIFont boldSystemFontOfSize:17.0f];
if([propertyItems objectForKey:#"online"] == [NSNumber numberWithInt:1])
cell.fio.textColor = [UIColor colorWithRed:0.3 green:0.6 blue:0.3 alpha:1.0];
dispatch_queue_t downloadPhotosQueue = dispatch_queue_create("downloadPhotosQeue", NULL);
dispatch_async(downloadPhotosQueue, ^{
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.pic.image = [UIImage imageWithData:photosData];
});
});
cell.fio.text = [propertyItems objectForKey:#"fio"];
I'm doing this in cellForRowAtIndexPath: method.
Everything is loading fast. But the problem is, when I'm scrolling my table down, and after again up, the images are reloading over and over again.
Question: Is there any way to easily cache my UIImages that I'm getting from the server? So if they are loaded once, they wouldn't reload over and over again, while I'm running the app. Maybe I'm doing something wrong?
Thanks in advance!
I strongly recommend AsyncImageView. I use it and it works like a charm. Just set the image url and it handles everything itself.
AsyncImageView *imageView = [[AsyncImageView alloc]init];
imageView.imageURL = [NSURL URLWithString:#"https://www.google.es/logos/classicplus.png"];
It caches the image in memory so it won't retrieve it from the server again. It will also release them when receiving a memory warning.
To load image from a website I use AFNetworking. It provides a class to load an image from an URL:
Start by adding #import "UIImageView+AFNetworking" to the top of a
controller or a view implementation file. This category adds methods
to UIImageView, like:
[imageView setImageWithURL:[NSURL URLWithString:#"…"]];
i recommend this library to accomplish what you want.
in cellForRowAtIndexPath:
[cell.imageView setImageWithURL:
[NSURL URLWithString:[NSString stringWithString:#"www.yourimagepath.path"]]
placeholderImage:[UIImage imageNamed:#"no_image.jpg"]];
the library will cache the image for you. you can also set the setHolder of the image, so it wont look like a blank image while the images are downloading.
The problem is here :
NSData *photosData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com/%#", [propertyItems objectForKey:#"photo_trumb"]]]];
dataWithContentsOfURL
It launch a request to the Url everytime the function is called (everytime you scrolled).
You need to load all the picture before cellForRowAtIndexPath.
Like in ViewDidLoad.
You can just store them in an array and display your picture's array in cellForRowAtIndexPath.
If it's really fast to load like you say : jsut load picture Once in CellForRowAtIndexPath. Store them in a mutableArray. And check if the picture already exist .
Here's my code so far (in the draw rect):
// Drawing code here.
NSLog(#"%#", [[NSBundle mainBundle] pathForResource:#"NoiseBGMainView" ofType:#"jpg"]);
NSURL *pathToBGImage = [[NSURL alloc] initWithString:[[NSBundle mainBundle] pathForResource:#"NoiseBGMainView" ofType:#"jpg"]];
NSImage *NoiseBGMainView = [[NSImage alloc] initWithContentsOfURL:pathToBGImage];
[NoiseBGMainView drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
And the image doesn't draw... I don't know what's wrong.
Why not just use [NSImage imageNamed:#"NoiseBGMainView.jpg"] instead? That will give you an NSImage of your picture (if it can find it), and nil if it can't. NSLog what the results of that call are, then come and report back on whether it's logging (null) or not.
You can draw an NSImage into an NSView without writing any drawing code by attaching an NSImageView to your view and setting your image resource as the NSImageView's image. (NSImageView handles all the drawing).
If your custom NSView is loaded from a nib, you don't need any setup code either - just make the appropriate NSView-NSImageView-NSImage (resource) connections with Interface Builder.
NSURL *pathToBGImage = [[NSURL alloc] initWithString:[[NSBundle mainBundle] pathForResource:#"NoiseBGMainView" ofType:#"jpg"]];
This is wrong. -[NSURL initWithString:] takes a string version of a URL, such as http://apple.com/, not a pathname.
Because you didn't pass a URL, the URL will be a relative URL, but a relative URL needs a base. The URL is a valid object, but doesn't lead anywhere. This, in turn, causes initWithContentsOfURL: to return nil (because you tried to load an image from a relative URL with no base). You then try to tell the image to draw, but you don't have an image, so nothing happens.
You want initFileURLWithPath:. (Or the class method fileURLWithPath:, with which you won't have to release the URL later.)