Please see this simple CATiledLayer example https://github.com/seanhess/CATiledLayer-Example
It consists of one viewController with a hierarchy like this:
view: (frame = window size)
scrollView: (frame = window size, content size = 200 x 4000)
contentView: (frame = content size = 200 x 4000, tile size = 100 x 100)
The content view's layer has been overriden to be a CATiledLayer.
If you run the linked code, you'll see that tiles with the same rect are requested multiple times. It happens both when you first run the code, and when you scroll.
Switch to branch "one-column" - it only happens on init, never when you scroll down.
Switch to branch "default-tile-size" - it only happens on init, but very rarely (you have to run it multiple times before it happens)
I'm trying to write some code in drawLayer:inContext: that locates the correct data and draws it. It could be expensive, and I don't want to do it more than once.
Any idea what is going on? What could I do differently?
This is a bug in IOS. It happens when the CPU is dual-core, in that case there are two threads each requesting each tile. That means that the bug is present in the simulator, and on the iPhone 4S, but not on the other iPhone models. I assume it will also be present on dual-core iPads.
I have reported the bug to Apple long ago already (for the simulator) and recently again (for the iPhone 4S). Apple recently gave the impression that they have solved it in IOS 6.
Very well solved here:
https://stackoverflow.com/a/8783396/341994
The problem is that (at least in my testing) drawRect: is called simultaneously on two threads. By logging, I can see individual lines of my code being executed twice before we go on to the next line, which is also executed twice! The logging shows that this is because one thread is executing a line and then another thread is executing the same line - and then they both go on to the next line.
This is as nasty as it gets. The solution is to wrap the the whole interior of drawRect: in a dispatch_sync on a serial queue.
Have you tried your code on a device? I consistently see this behavior in the simulator, but not on my iPad 1. I just tried this again in an app that is displaying a large tiled image. My tile size is 256 and on the device my drawRect: method is being called 12 times, which is how many tiles are on the screen. In the simulator, the method gets called between 20 and 23 times, with all but the last few tiles being drawn twice.
Isn't this due to the setLevelsOfDetailBias ? When I leave everything standard the tiles only get rendered once (testing on the device). But when I set the levelofdetailbias to 2, the tiles get loaded multiple times. I think that's due to the fact that CATiledLayer just starts caching extra detail levels when the device is idle.
In the end, the answers didn't work. It is fairly simple, though, to make your delegate method be aware of whether or not it has drawn that cycle.
Related
I have a UIView in which I display a movie if a particular chapter demands it. This view is resized via constraints in Main.storyboard to adapt to iPhone. All works fine on iPad. It also works fine on iPhone unless the app is asked to reload when a movie containing chapter was last active meaning that a movie will be first-up on loading. In this scenario, the movie is loaded into the iPad dimensions instead of the smaller iPhone specs.
It appears that the constraints on the movie's view are not engaged in a timely manner. The issue centers on a query of the the view's bounds. If I insert a delay before using the bounds the issue goes away. In fact, a delay of 0.0 seconds does the job!
Using a kludge that relies on a delay seems pretty funky to me. I can also move the call that uses the bounds to viewDidLoad to resolve the issue but then I see some underlying “garbage” when reloading the app, the launch image seemingly not in effect. Any suggestions?
It sounds like you're using bounds before your views have been lain out. Without actually seeing your code, I would suggest executing your described code inside of viewDidLayoutSubViews().
I have a 6000x3000 px image that is in a zoomable view in my xCode project. In the initial view, a button is pressed to access the view with the large, zoomable image. This all works fine, except for the time that is taken to "load" the image often times causes a crash in the app, especially when I am testing on older devices (it seems to work fine most of the time on my 4G itouch). Is there any way to "pre-render" this one large image, or anything else that I can do to prevent crashing?
Do the math: 6000 x 3000 x 3 (red green blue) = 54,000,000 bytes = 51.5MiB of raw data. The normal image handling has a lot of overhead and that simply takes too much memory.
According to this question the solution is to use a CATiledLayer. As far as I have understood it, you need to divide your large image into smaller parts and draw these smaller parts with the help of CATiledLayer.
Edit: Here's a quote from the UIView class reference:
Note: In iOS 2.x, the maximum size of a UIView object is 1024 x 1024 points. In iOS 3.0 and later, views are no longer restricted to this maximum size but are still limited by the amount of memory they consume. It is in your best interests to keep view sizes as small as possible. Regardless of which version of iOS is running, you should consider tiling any content that is significantly larger than the dimensions the screen.
Read: If it's larger, use a CATiledLayer to draw smaller parts.
I'm using core-plot in my iPhone app to display datas in plots from NSMutableArrays.
I display 10 values on the screen and I allowed horizontal scroll only like that I can see all the values when I'm scrolling.
I've got something like 100-200 values in total to load.
I use differents tabBars to handle my differents needs.
Everything works fine, but when I launch my app on the iPhone I have some monstrous laggs when scrolling. Something like 2 seconds after the graph move (on the simulator there is no laggs).
I reload graph when I shake the iPhone.
I work on a 3GS, and there is no other app running in background.
Did someone had and resolved something like that?
Do you need more info?
UITable (and UIScrollView) has a bug in which it causes it to call layoutSubviews on its parent. Check to make sure this isn't causing your trouble. If it is, you may be able to solve it by wrapping the table in another UIView. I do this routinely to avoid this problem.
Another thing that can cause really slow scrolling is a lot of masks, transparencies, etc in your cells. For this, you might try setting the views to rasterize, which means they'll draw themselves once into a buffer, rather than draw themselves every time.
I'm developing an app which uses a UIScrollView to show a list of images based on search criteria. Using a button the user can load more images.
When testing on an iPhone 4 the ViewController receives a memory-warning at ~750 images. When testing on an iPod 2nd generation a memory warning is received at ~150 images.
My understanding is that when didReceiveMemoryWarning is called, one can free memory by releasing objects but recovery from low memory is not guaranteed.
I've implemented didReceiveMemoryWarning and release basically all objects. In instruments I see memory usage drop back to ~3MB. The first time the iPod reaches its memory limit all goes well, memory is released and the app resumes normal operation. The second time however, when didReceiveMemoryWarning is called, I can see the objects released, but the app crashes anyway.
So, how do I make my app crash proof? I want to make sure that all devices running the app can load as much images as memory allows but I also want to make sure that the app doesn't crash.
I would prefer the app never to reach didReceiveMemoryWarning and set a limit to the number of images that can be displayed, but how can I determine how many images each possible device should be able to load?
Furthermore, the size of the images is not guaranteed. While testing I come to this arbitrary number of 150 on an iPod, but what if the images on the server at some point in time are twice as big? Then the app would probably crash at 75 images.
Any sugestions?
First off, what you probably want to do is not display all your images all at once. You rather, probably only want to disable the images that are currently visible, plus a few that are off screen preloaded for when the user scrolls to that location.
This is much the same way as the photos app works, how UITableView is implemented. Basically it boils down to this:
You have your main scrollview, and inside it, you have individual cells. These cells are small views that are added as subviews to your scrollview at specific offsets. You then add your images to these cells.
When a user scrolls the scrollview, you first ask the scrollview to dequeue a new cell for you, much the same way you'd ask a table view to dequeue a cell for you to use. This saves the cost of an allocation, if one has been recycled. If you cannot dequeue one from a recycled set, then what you have to do is quite simple: allocate one as you are doing currently.
Furthermore, to implement this cell recycling, what you need to do is see which cells are visible on screen. If one or more cells go off screen, you add them to the recycled cells NSSet which you create. This set just holds cells for later recycling. There's some sample code Apple has which demonstrates this, and it's called PhotoScroller. It also is demonstrated in a WWDC10 video, session 104. I suggest you watch it. Ignore the parts about tiling, you don't need to know that for your purpose.
Once you have this in place, this will force you to set up your cells only when they're needed, which is also another key aspect of this behaviour.
Finally, when you do receive a memory warning, just drop the recycled cells set. If you ever get high enough that this matters, that is, you'll save a few megs of memory. :) (Do not forget to implement it though, ever, when you hold onto temporary data that you don't specifically need around...cells which are not visible on the screen are a good example of this, as are caches.)
You should lazy load your images and only load the images you need at that time. Your app can't show all those images on one screen anyways, so on your scrollview you should only load those images that can fit on the screen and maybe a few around that, and as the user scrolls to release the images it no longer needs.
I'm trying to workaround a long-running bug in Apple's SDK here, but I can't see how to achieve it without huge amounts of code.
Here's the bug:
Create a view.
Put another view inside it, with an origin ANYTHING except (0,0).
Configure the subview to resize to fill
...then, at runtime:
4. Set the superview size to zero
5. Set the superview back to ANY non-zero size
BANG! Apple's SDK goes haywire, resets the origin of the subviews, loses all ability to tell up from down, etc. (seems like the programmer "Forgot" to include a variable to record what the spring/strut-size was before it went to 0).
I've heard this bug has existed in OS X for 5 years or so, and Apple still hasn't fixed it. What I would like is some way (any way!) of working around it, without re-writing Apple's entire springs/struts system and implementing it properly, without the bug. Unfortunately, I can't even find the original references to the OS X bug that someone showed me previously.
EDIT:
...I have a resizing "drawer" at the bottom of the screen that has to shrink to small height. The problem is subviews "collapsing" all down to origin 0 (amongst other things) at the drop of a hat (technically: they're reducing their origin.height, but not re-increasing it).
The best solution I have found is to limit your minimum window size so that none of your views can ever hit zero size.
EDIT: an hour later, the collapsing behaviour has returned, without me touching the source files. I'll use source-control to double-check, but I believe all that happened is I added other files to a different part of the view-hierarchy and re-built. Argh!
(what follows SEEMED to work, but is now failing again, in the same way)
Hmm. So, I had two UIVC's on one screen. Refactoring so that I only had one made the resizing bug vanish. It seems to be a bug in the UIVC layout code.
NB: I had the idea this might be a problem because I know Apple currently advises you to only have one UIVC on screen at once with iPhone, even though this means you have to ignore their MVC architecture for complex apps. So, technically, they warn you away from this. But, at the same time, I'm now unable to use the MVC stuff for this code.
The following worked (for my current example - may not be universal!).
HOW:
If you're adding any UITableViewController instances ... give up. You'll have to re-implement that class yourself
Find my subviews that were in UIVC instances, and change the extended class in the UIVC header from "UIViewController" to "NSObject"
Move all code from viewDidLoad to init (remembering that "init" has special rules about first few lines etc)
Replace the instantiation of that UIVC from "[[vc alloc] initWithNibName:nil bundle:nil]" with a plain "[[vc alloc] init]"
Another potential workaround (that works in my current situation):
The only View I have right now that MUST have a non-zero origin and MUST resize-to-fit is a UITableView.
Apple has a special feature of letting you put any view as a "table-header".
So ... I've taken all my other components, stuck them in a large view, and made that the table-header view.
The downside is that they now scroll off the top when you scroll the table, but it allows me to give the table a zero-origin, working around the SDK bug.