Swift 4: Image inside UIView only display after second UIViewController call - swift

I inserted a UIView with a image inside a Cell View like tha image bellow.
What i tried:
DispatchQueue.main.async {
self.hideProfileDescriptionView.addSubview(self.hidedescriptionLogo)
self.hideProfileDescriptionView.bringSubview(toFront:
self.hidedescriptionLogo)
self.cellDescriptionView.addSubview(self.hideProfileDescriptionView)
}
In the First time I call the UIVIewController, the image does not display, but in the second time I call UIViewController the image appears.
How can I solve that.
Thanks

In the first case Image didnt load fully and second time download is completed. If you dont to wait this installations You can use KingFisher repo https://github.com/onevcat/Kingfisher .
This repo is highly recommended. Also with this repo you can put laoder in UIImageView.

I realized that all cell content was not being displayed, so i discovered a flag that i did put on the code and i did disable that. Solved.

Related

Unexpectedly found nil when opening an UIImage

I am developing a Walkthrough screen where I have added 3 instruction images which help the users to work with the application. The problem occurs, when I first launch the app I see only the labels with the "instruction tekst" , but below this label the user should also see the images. Maybe I am doing something wrong, attached I uploaded a picture of the error:
And here I upload the picture of the code I have used to transfer these picture to the TutorialPageContentHolderViewController:
I would be very thankful if you see my mistake, because I have been struggling with that the whole day.
I guess the imageFileName variable in your first image is nil when you use it in
myImageView.image = UIImage(named: imageFileName)
When you assign it a value in the second screenshot it could be too late, as viewdidLoad is loaded before that. You could check if this is true by manually assigning a valid value to imageFileName before using it and see if it works.
One simple way to fix it would be to add an initialiser to the view controller class, pass the filename as a parameter and store the filename in a local variable until you use it in viewDidLoad.
A second way would be to add a method to the page view controller as below (and remove the corresponding line from viewDidLoad):
func prepare(with filename:String) {
myImageView.image = UIImage(named: filename)
}
and call it from the pageTutorialAtIndex method in your main view controller.

how to change default image in UIActivityIndicator by SWIFT

In my app activityIndicator is necessary. But, i don't want that default symbol. If i want to replace any other image by using SWIFT means, how can i? Kindly help me.
You can't customize the spinning thing in a UIActivityIndicator. What I suggest is that you make your own indication of activity; for example, use an animated UIImage:
let im = UIImage.animatedImageNamed("myImages", duration:1)
... and show it (in an image view, probably) as a way of suggesting that activity is occurring.

How to populate an image in UITableViewCell even if user scroll the table

I'll try to explain my self, I have ContactsViewController that shows a table view with a list of contacts (the model is an array of Contact objects), each cell display an image of a contact.
Currently what I do to populate the cell's UIImageView is this:
1. I override the Contact image property getter -
- (UIImage *)contactImage
{
if (!_contactImage) {
_contactImage = [[UIImage imageNamed:#"placeHolder.png"] retain];
[self asyncDownloadContactImageFromServer];
}
return _contactImage;
}
Then when I finish downloading the image I set it to the contactImage property and I post a ContactUpdatedImageNotification.
My ContactsViewController then get this notification and reload the cell of this contact, this will set the downloaded image to the cell's imageView.
The result of this is good async fetching of the images without blocking the UI while the user scroll the table view.
BUT there is something small that bothers me, when the a user scroll the table view and reveal new cells the new cell's image get download as expected but the cell's imageView is not updated with the new downloaded image till the user pick up his finger.
I supposed that I need to do something in another thread to make this effect, but I don't know how?
The image is not updated until the user stops scrolling due the code being executed in the default runloop, which gets delayed until scrolling finishes. This other question deals with the difference between the runloops, NSDefaultRunLoopMode vs NSRunLoopCommonModes and it precisely recommends not updating the images while scrolling since that can introduce jerkiness in the scrolling itself if you are not careful.
Also, now that you know about the existence of these runloop modes you will be able to find much more information about them in the xcode documentation or internet.
Hey Eyal visit following url...you will get answer and as well sample code...
tableview with different cell with different images
Hope this will help you...

Where to put cleanup code in UIViewController?

I have a UIViewController subclass that loads a bunch of images for each cell in a tableview asynchronously which is handled by a separate download class. I keep a list of all of these download requests in a dictionary which is keyed to the index of the cell that is requesting the image.
My question is i where should i put the code that cancels the image download if the viewcontroller is popped off the navcontroller? I need to do this because if the user hits back while there are still images being downloaded (which could take a while) then when they are finished downloading the viewcontroller has already been released.
I cant put it in the viewWillDisappear method because i do not want to stop the download if the user clicks on a separate tab and only when the hit the back button. For now i put this code in the viewcontrollers dealloc method which works fine although it doesnt seem right for some reason. I thought of using the viewDidUnload method but it seems this is only called when there is a low memory warning?
Any ideas?
dealloc is the perfect place for this as the view controller gets deallocated and you are responsible to clean your stuff up.
You may want to consider putting the code in viewWillDisappear: anyway, after all if the screen you are navigating to needs anything loaded it will be slowed by the background image load...

TTTableImageItem doesn't load the image until scroll

I'm using the three20 project for my iPhone app. I've narrowed my problem down and I'm now just trying to re-create the 'Web Images in Table' example that comes with the project. I've copied the code exactly as in the project, with the exception that I do not use the TTNavigator (which the example does) but I am adding my TTTableViewController manually to a tabBar.
The problem is as follows; the images in the table should load automatically from the web, like in the example. But they only load after I scroll the table up and down.
In the console it clearly says it is downloading the images, and you see the activity indicator spinning like forever.. And unless I scroll up and down once, the images will never appear.
Anyone? Thanks in advance.
P.S:
If I'm using this code in any random UIView, It also doesn't work (only shows a black square):
TTImageView* imageView = [[[TTImageView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)] autorelease];
imageView.autoresizesToImage = YES;
imageView.URL = #"http://webpimp.nl/logo.png";
[self.view addSubview:imageView];
If I put this code in my AppDelegate (right onto the window), it does work .. strange?
POSSIBLE SOLUTION:
Although I stopped using TTImageView for this purpose, I do think I found out what the problem was; threading (hence accepting the answer of Deniz Mert Edincik). If I started the asynchronous download (because basically that is all the TTImageView is, an asynchronous download) from anywhere BUT the main thread, it would not start. If I started the download on the main thread, it would start immediately..
Sounds like a threading problem to me, are you creating TTImageView in the main runloop?
I find one interesting thing. When I use combination TTTableViewController, TTTableViewDataSource and TTModel I have same problem with loading TTImageView. My problem was, that my implementation of Model methods 'isLoading' and 'isLoaded' don't return proper values after initialization of model. That forces me to call reload on model manualy in 'viewDidAppear' method and that causes image loading problem. So I repair my 'isLoading' and 'isLoaded' methods to both return 'NO' after Model init, and everything is fine.
When an image finishes loading try sending a reloadData message to the table view. This forces the table to recalculate the size of the rows and redraw the table. Just be careful that you don't start downloading the image again in response to this message.
I've written something similar to this where an image view will load its own image from the web.
Im my experience, when the image had loaded successfully but was not shown in its view, it was a case that the cell needed to be told to redraw.
When you scroll the table view, the cells are set to redraw when the come onscreen, which is why they appear when you scroll.
When the image loads, tell the cell that it is sitting in to redraw by sending it the message setNeedsDisplay.
That way, when the image finishes downloading, the cell its sitting in (and only that cell) will redraw itself to show the new image.
It's possible that you might not need to redraw the entire cell and might be able to get away with simply redrawing the image view using the same method call. In my experience, my table cells view hierarchy was flattened, so I had to redraw the whole cell.
I don't have an answer for what you want to do, but I will say that this is considered a feature, and the expected behavior. You use TTImageView in UITableView when you want to do lazy loading of images. TTImageView will only load the images whose frames are visible on the screen. That way, the device uses its network resources to download images that the user has in front of them, rather than a bunch of images that the user isn't even trying to look at.
Consider having a long list that may contain a couple hundred thumbnail images (like a list of friends). I know from experience that if you kick off 100+ image requests on older devices, the memory usage will go through the roof, and your app will likely crash. TTImageView solves this problem.
This is a thread problem. You can load the images by including the line:
[TTURLRequestQueue mainQueue].suspended = NO;
in - (void)didLoadModel:(BOOL)firstTime.