简体   繁体   中英

Keeping an image in view after removed from superview

I have extracted some code from the PhotoScroller sample code from Apple to use the CATiledLayer . I have an image of 8000x7000px loaded from internet, in tiles.

This is kind of a map-function in my app. I also have two almost identical images, with different overlays (tried to only add the overlay without luck).

I have an UISegmentedControl to toggle between the three choices, and I want the imageView to load the images from the selected image. So if the user zoomes in on one of the images, and selects another option, then the scale and coordinated stays the same, and the imageView loads the chosen image in the necessary tile-spots.

I have partially managed this. Or, I have actually managed this, but when I select another option, the whole screen goes black when the function [imageView removeFromSuperview]; is called. After being black in a couple of seconds(depending on internet speed) it shows the correct tiles.

I want the layer to "fade" over to the next layer if possible. As you maybe know, when using CATiledLayer , the first layer is the whole image in low resolution, but when zooming in, the necessary tiles "fade" in to the next layer with smaller tiles.

I basically need to give the (TilingView*)imageView a "reload"-command, and want it to "fade" over the last image.

I tried to comment out the [imageView removeFromSuperview]; , and that actually got me close to what I want. When I zoomed in, and selected another option, the new image actually faded over the old one, however, when I zoomed back out, the old image was clearly sticking around in the background(behind the new image), not responding to anything. I need to remove it from the superView at a later point, but I have no longer access to it as the new image has taken its place as imageView. I know people might want to see code here, but I have really no idea what code to show.. And the CATiledLayer is SO POORLY documented I am really having a hard time understanding what's going on.

I made it work through some sketchy code. There probably are other better solutions, but this is the only one I know of at the moment.

The problem was that when I removed [imageView removeFromSuperview]; , it did not unload the last image, thus letting it be in the memory forever. As it was not affected by zooming and scrolling, when I zoomed out, it was always there in the background. If I was to switch between options multiple times, multiple images would add up in the background.

I simply created a " helper " as I call it. A new imageView , which gets the content of the old imageView when switching option, without removing it from the superview just yet, as a premature removal results in black screen when the new image hasn't loaded quite yet.

I now call the [helper removeFromSuperview]; in the delegate methods scrollViewWillBeginZooming AND in scrollViewWillBeginDragging . This results in memory containing at the most two images in the time between clicking the other option, and user scrolls or zoomes.

Note: If the user scrolls or zoomes immediately after switching option in the UISegmentedControl , it turns black because the new image hasn't been loaded yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM