iPhone animation with stretchable image - iphone

I got a UIView subclass that draws a UIImage as its background. The image is created using the stretchableImageWithLeftCapWidth:topCapHeight: method, it has round edges.
I also use an animation block inside of which I resize my view. I had hoped that during animation, the drawRect:method would get called sometimes, which would result in the background image getting drawn correctly.
Unfortunately, the animation seems to render the image and then just rescale it during the animation, which obviously makes the formerly round edges getting nastily stretched.
The only workaround I can imagine is put three separate UIImageViews (top cap, middle fill, bottom cap) above my original background image and then reposition the caps images and scaling the fill image. However, this seems quite complicated...
Is there any better way I can prevent this from happening?
EDIT: Found this. Sounds bad...

While digging through some Apple docs, I coincidentally found the solution to my problem. All you have to do is play with UIView's contentStretch parameter. It allows you to manually specify the area of your view that gets stretched during animation.

Related

How do I blur (ios7 style) a section of an image in a UITableViewCell?

I've got a UIImageView that takes up the whole cell and shows an image. I'd like to blur the bottom third of the view in the iOS7 style (we're targeting iOS7). There will be a label over the blurred part.
The issue seems to be that I can't "screenshot" the UIImageView right as I am setting up the cell in tableView:cellForRowAtIndexPath: as it hasn't loaded yet. Although i've even tried setting up a timer for a 0.1 second delay and that also doesn't work.
I've tried the stuff on http://damir.me/posts/ios7-blurring-techniques
I've tried https://github.com/justinmfischer/7blur
I'd like to use the new screenshotting API drawViewHierarchyInRect:afterScreenUpdates:
I'd also like to use apple's sample code UIImage+ImageEffects
The only thing that has worked so far has been to just put a UIToolbar in but the performance is really slow. I don't need dynamic updates.
I've managed to get the following effect a few times but it has just tinted the view and hasn't actually blurred anything so I'm a bit confused.
Use UIImageEffects sample from apple
Make a Image View with a size of the lower portion you want to blur... and then set its image to a blurred one. also set its content mode to UIViewContentModeBottom for the proper clipping
Since your image isnt the size of the cell.. first get a temp image as it is being displayed in the cell by drawing a UIImage from it.. refer for the code here
How to capture UIView to UIImage without loss of quality on retina display
then blur that image
Load this image into a UIImage, blur the image, clip to fit the area that you want. Use UIImage+blur.h

Creating translucent UIView with gaussian blur

I am in the process of creating an app where I have a background image (a UIImageView with the frame set to the UIView's bounds and added as a subview).
I would like to now add a UIView that will be over the background image but will apply gaussian blur to the image behind, only over the area where the UIView is.
I have just tried to use GPUImage to create a blur, which looks good but it uses 2 UIImageViews (one large background and one behind the UIView, which is blurred). Unfortunately the image no longer matches up with the background.
Is there any way to get the UIView to blur only the area behind it? A bit like the UIView is frosting the image behind.
Thanks!
Try FXBlurView from here - https://github.com/nicklockwood/FXBlurView
It should work.

Unable to add oversized button in UIToolbar

I have a UIToolbar in IB with a custom UIBarButtonItem that is set to an image. The image height is larger than the UIToolbar height. The image does not load for some reason. Attached is a screenshot.
You should avoid using subviews that are larger than the parent view - even if it does work and draws the view outside of the bounds, you cannot always count on it because the clipping is skipped for performance reasons and depending on the drawing order the overlapping part of the subview might be covered later. Second problem is that only the part within the frame of the superview is able to handle the touch events.
As to why you don't see your image, it's probably a different problem, I doubt it has something to do with it going outside the frame. Post your code to get more feedback ;)

How do I blur whatever is behind a translucent UIImageView?

I have a translucent png image (made by turning down the opacity in Photoshop), and there's a UIImageView behind that translucent image. Is there any way to blur the parts of the UIImageView on the bottom that intersect with the UIImageView on top? Thanks!
Not aware of any code that will do that on its own, but you could 'fake it' by rendering the intersection of the views in a graphics context, then apply a blur effect to it. Then you could take the graphics context and insert a UIImageView with the resulting image in between the two views to simulate the effect. This method would not work very well if you have any sort of animation or otherwise changing the state of the images.
You may try setting alpha of the image to less than 1.

CATiledLayer blanking tiles before drawing contents

All,
I'm having trouble getting behavior that I want from CATiledLayer. Is there a way that I can trigger the tiles to redraw without having the side-effect that their areas are cleared to white first? I've already subclassed CATiledLayer to set fadeDuration to return 0.
To be more specific, here are the details of what I'm seeing and what I'm trying to achieve:
I have a UIScrollView with a big content size...~12000x800. Its content view is a UIView backed by a CATiledLayer.
The UIView is rendered with a lot of custom-drawn lines
Everything works fine, but the contents of the UIView sometimes change. When that happens, I'd like to redraw the tiles as seamlessly as possible. When I use setNeedsDisplay on the view, the tiles redraw but they are first cleared to white and there's a fraction-of-a-second delay before the new content is drawn. I've already subclassed CATiledLayer so that fadeDuration is set to 0.
The behavior that I want seems like it should be possible...when you zoom in on the scrollview and the content gets redrawn at a higher resolution, there's no blanking before the redraw; the new content is drawn right on top of the old one. That's what I'm looking for.
Thanks; I appreciate your ideas.
Update:
Just to follow up - I realized that the tiles weren't being cleared to white before the redraw, they're being taken out entirely; the white that I was seeing is the color of the view that's beneath my CATiledLayer-backed view.
As a quick hack/fix, I put a UIImageView beneath the UIScrollView, and before triggering a redraw of the CATiledLayer-backed view I render its visible section into the UIImageView and let it show. This smooths out the redraw significantly.
If anyone has a better solution, like keeping the redraw-targeted tiles from going away before being redrawn in the first place, I'd still love to hear it.
I've found that if you set levelsOfDetailBias and levelsOfDetail both to the same value (2 in my case), then it only redraws the tiles that are touched by my setNeedsDisplayInRect: call, as you'd hope.
However if the levelsOfDetail is different to LODB, then any calls to setNeedsDisplayInRect: redraw all the tiles.
You could add another layer (possibly a CATiledLayer) behind the existing tiled layer. (Sort of a double-buffered solution.) You would call setNeedsDisplay: on the second layer from a timer that fires after a few seconds to ensure that that layer doesn't redraw at the same time as the front layer.
Another potential option is to use the same delegate to draw content to a bitmap context and swap the bitmap into the backing store once the content is refreshed. This should produce a flicker-free result. That being said, I can't tell you how this might be done, and one nice thing about CATiledLayers is they automatically generate tiles when you zoom and pregenerate tiles when you pan once zoomed in.
I would like to see how you implement your application. I have been looking for weeks to find an example that uses a combination of UIScrollView and a CATiledLayer-back view with a lot of custom drawn lines. Apple has some great sample code - but it all involves images rather than line art, so no help for me.
Having read through these answers without a solution, I discovered that tiling a page was the dominant background task.
Preparing my lo-res placeholder image on a high priority queue solved this issue - the images now appear while the tiling is occurring. Caching the placeholder images further improves their appearance - they appear before the tiling begins.
With newer devices, the tiling it so fast, these tricks might not matter. A sample PDF consisting of large scanned images (e.g. a scanned book) tiles the slowest in my experience, and makes for good test data.
I had the same problem with iPad.
The solution was more simple than I thought and far more simple than using UIImageView to render display before redrawing... :
Just don't set any background color for the Layer!
I had CATiledLayer set in a similar way:
layer = [[CATiledLayer alloc] init];
layer.masksToBounds = YES;
layer.contentsGravity = kCAGravityLeft;
//layer.backgroundColor = [[UIColor whiteColor] CGColor];
layer.tileSize = CGSizeMake(1004.0, 1004.0);
layer.levelsOfDetail = 16;
layer.levelsOfDetailBias = 8;
Note that I have commented out the line setting layer's background color to white.
After that the white blank before redraw problem disappeared!
Let me know if anyone has tried that.