drawRect: crash by big rect after zomming - objective-c

My UIView structure:
I have a "master" UIView (actually UIScrollView, but it's only purpose scrolling per pagination).
As a subView on this "master" I have my "pageView" (subclass of UIScrollView). This UIScrollView can have any content (e.g. a UIImageView).
The "master" has another subView: PaintView (subclass of UIView). With this view, I track the finger movements and draw it.
The structure looks like this:
[master.view addSubview: pageView];
[master.view addSubview: paintView];
I know when the user zooms in (pageView is responsible for this) and over delegate/method calls I change the frame of paintView according to the zoom change, during the zoom action.
After zooming (scrollViewDidEndZooming:withView:atScale) I call a custom redraw method of paintView.
Redraw method and drawRect:
-(void)redrawForScale:(float)scale {
for (UIBezierPath *path in _pathArray) {
//TransformMakeScale...
}
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect {
for (UIBezierPath *path in _pathArray) {
[path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
}
The problem:
When I zoom-in, I receive a memory warning and the app crashes.
In the Allocations profiler I can see that the app own a lot of memory, but I can't see why.
When I don't call 'setNeedDisplay' after my 'redrawForScale' method the app isn't crashing.
When I log the rect in drawRect I see values like this: {{0, 0.299316}, {4688, 6630}}.

The problem is (re)drawing such a huge CGRect (correct me if this is wrong).
The solution for me is to avoid calling a custom drawRect method. In some stackoverflow answers (and I think somewhere in the Appls docs) it was mentioned that a custom drawRect: will reduce the performance (because iOS can't do some magic on a custom drawRect method).
My solution:
Using a CAShapeLayer. For every UIBezierPath I create a CAShapeLayer instance, add the UIBezierPath to the path attribute of the layer and add the layer as a sublayer to my view.
CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] initWithLayer:self.layer];
shapeLayer.lineWidth = 10.0;
shapeLayer.strokeColor = [UIColor blackColor].CGColor;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.path = myBezierPath.CGPath;
[self.layer addSublayer:shapeLayer];
With this solution I don't need to implement drawRect. So the App won't crash, even with a big maximumZoomScale.
After changing the UIBezierPath (translate, scale, change color) I need to set the changed bezierPath to the shapeLayer again (set the path attribute of shapeLayer again).

Related

Objective C - OS X - Issue adding NSShadow to NSImageView

I am trying to add a shadow to a NSImageView on an MAC application.
I created a custom NSImageView class "ShadowView.h" and modified the drawRect: like so:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:5];
[shadow setShadowOffset:NSMakeSize(30.0, 3.0)];
[shadow setShadowColor:[NSColor redColor]];
[shadow set];
[self setWantsLayer:YES];
[self setShadow:shadow];
}
However nothing happens. Also, when I debug I can see the above code being called. I looked at this question from 5 years ago but it seems to not work anymore
Adding a Shadow to a NSImageView
Thank you!
When adding a shadow to a view, that view's superview also needs to have layer-backing enabled. If it doesn't, the view's shadow gets clipped at its own bounds, as seen in this sample app:
Make sure you call -setWantsLayer:YES on your view's superview (or check the "Core Animation Layer" checkbox in Interface Builder) in order to make sure the shadow is completely visible:
You should set these somewhere else like, initWithFrame: take them out of the drawRect:
[self setWantsLayer:YES];
[self setShadow:shadow];

Subviews of NSRect not affected by NSAffineTransform

When applying an NSAffineTransform rotate and translate to an NSRect, none of the sub views are affected by the transform. I've included the code below.
- (void)drawRect:(NSRect)dirtyRect
{
NSAffineTransform * transform = [NSAffineTransform transform];
[transform translateXBy:13.0 yBy:3.0];
[transform rotateByDegrees:-45];
[transform translateXBy:-13.0 yBy:3.0];
[transform concat];
NSImageView *imageViewWatchPointer;
NSRect watchPointer = NSMakeRect(0, 0, 134, 49);
imageViewWatchPointer = [[NSImageView alloc] initWithFrame:watchPointer];
[imageViewWatchPointer setImageScaling:NSScaleNone];
[imageViewWatchPointer setImage:[NSImage imageNamed:#"mastery_watch_pointer"]];
[self addSubview:imageViewWatchPointer];
[[NSColor blueColor] set];
NSRectFill(watchPointer);
}
You're not applying transform to anything. You're creating it and then throwing it away. In any case, you never add subviews in drawRect:. drawRect: is for custom-drawing the current view, not for managing the view hierarchy. The way you've done this, you're adding a new NSImageView every time the main view is redrawn.
I recommend going back to the Cocoa Drawing Guide to understand how Cocoa drawing works.
Looking at your code, I don't believe you need drawRect: at all. You just want to add a subview, probably in awakeFromNib or initWithFrame:. I assume you want the view rotated. The easiest way to do that is to give it a backing layer (setWantsLayer:), and then set the affineTransform on the layer.
Alternately, in drawRect:, you could rotate the context and just draw the image itself (without using an NSImageView). See the Cocoa Drawing Guide for details on how to do custom drawing.

CALayer not rendering contents

This one is stumping me. I have a CALayer which I create in a UIViewController's viewDidLoad method something like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage* img = [UIImage imageNamed:#"foo.png"];
_imageLayer = [[CALayer alloc] init];
_imageLayer.bounds = CGRectMake( 0, 0, img.size.width, img.size.height);
_imageLayer.position = CGPointMake( self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0 );
_imageLayer.contents = (id)img.CGImage;
_imageLayer.contentsScale = img.scale;
[self.view.layer addSublayer:_imageLayer];
[_imageLayer setNeedsDisplay];
}
The UIViewController is loaded from a nib and placed into a UITabBarController that was created in code.
My problem is that the CALayer does not render its UIImage contents when the UIViewController becomes visible. The only way I can make the CALayer to render the UIImage is by setting its contents to the UIImage after the UIViewController is made visible, such as in the viewDidAppear: method.
I would really prefer to do all my set up in the viewDidLoad method (isn't that what's it’s for?). How do I get the CALayer to render its contents?
You don't need that setNeedsDisplay on the layer, or the superview.
setNeedsDisplay tells a layer or view that it needs to redraw its contents. (For a view, it will call drawRect: to get new contents; for a CALayer, it will ask its delegate.) Note that this affects only the view or layer itself, not its children.
Since you are providing the content for the layer directly, you don't want CA to discard that content and ask for a replacement.

Fade effect at top and bottom of NSTableView/NSOutlineView

I'm looking for a way to draw a fade effect on a table view (and outline view, but I think it will be the same) when the content is scrolled. Here is an example from the Fantastical app:
Also a video of a similar fade on QuickLook windows here.
To make this I tried subclassing the scrollview of a tableview with this code:
#define kFadeEffectHeight 15
#implementation FadingScrollView
- (void)drawRect: (NSRect)dirtyRect
{
[super drawRect: dirtyRect];
NSGradient* g = [[NSGradient alloc] initWithStartingColor: [NSColor blackColor] endingColor: [NSColor clearColor]];
NSRect topRect = self.bounds;
topRect.origin.y = self.bounds.size.height - kFadeEffectHeight;
topRect.size.height = kFadeEffectHeight;
NSRect botRect = self.bounds;
botRect.size.height = kFadeEffectHeight;
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setCompositingOperation: NSCompositeDestinationAtop];
// Tried every compositing operation and none worked. Please specify wich one I should use if you do it this way
[g drawInRect: topRect angle: 90];
[g drawInRect: botRect angle: 270];
[NSGraphicsContext restoreGraphicsState];
}
...but this didn't fade anything, probably because this is called before the actual table view is drawn. I have no idea on how to do this :(
By the way, both the tableview and the outlineview I want to have this effect are view-based, and the app is 10.7 only.
In Mac OS X (as your question is tagged), there are several gotchas that make this difficult. This especially true on Lion with elastic scrolling.
I've (just today) put together what I think is a better approach than working on the table or outline views directly: a custom NSScrollView subclass, which keeps two "fade views" tiled in the correct place atop its clip view. JLNFadingScrollView can be configured with the desired fade height and color and is free/open source on Github. Please respect the license and enjoy. :-)

Custom drawing on top of UIButton's background image

What is the method calling sequence of UIButton's drawing?
I have a UIButton subclass, let's call it UIMyButton. I add it to a .xib controller and give it a background image. Then in the UIMyButton.m file I override drawRect method and draw a shape.
Something like:
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [UIColor orangeColor].CGColor);
CGContextFillRect(c, rect);
}
The problem is that it draws the orange rectangle underneath the background image. What methods are responsible for drawing/updating background images (it updates on "tap down" for example)? How do I force the draw on top without adding an additional custom subview to the button (or is the background image a subview itself)?
I'm just trying to get a clear understanding of how the UIButton drawing sequence works.
The solution is to not use a background for the button. Instead, draw the image inside the drawRect method like so:
-(void)drawRect:(CGRect)rect
{
CGContextRef ctx=UIGraphicsGetCurrentContext();
UIImage *image = [UIImage imageNamed:#"image.jpg"];
[image drawInRect:self.bounds];
CGContextSetFillColorWithColor(c, [UIColor orangeColor].CGColor);
CGContextFillRect(c, rect);
}