Warning I can't get rid of: "No '-renderInContext:' method found"

Discussion in 'iOS Programming' started by robbieduncan, Mar 21, 2010.

  1. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #1
    I'm writing a free application for a club that I'm a member of (to allow members to carry an off-line, iPhone optimised version of our technical Wiki in case anyone is interested). It all works well but I have a single warning in my code that I can't get rid of and it's starting to bug me!

    Before I go into the code I should note that the warning is (obviously) compile-time but has no effect at runtime: the method is called and executes fine.

    The application is "themed" somewhat to match the colours of the club. So the navigation bars are yellow with green text (no comments on the taste aspect of this required :p). To get this I've had to work around some issues in the SDK. In particular getting the back buttons to display in green. For whatever reason you can't use initWithCustomView: to create a UIBarButtonItem and use that as your back button: the view you pass just get's ignored. But you can use initWithTitle:style:target:action: (to set a custom title but still in the default style) or even better for me use initWithImage:style:target:action:.

    Now I don't want to have to pre-render images for each and every possible back button text, especially as I don't actually know all the possible values at compile time. So what the code below does is create a UILabel, configure it to draw text in the style I want and then render that into an image.

    Code:
    UILabel *newBackView = [[UILabel alloc] init];
    newBackView.text = self.title;// Set the title
    newBackView.backgroundColor = [UIColor clearColor]; // Clear background
    newBackView.textColor = SELOC_GREEN; // Green text
    newBackView.font = [UIFont boldSystemFontOfSize:12]; // Correct bold font
    newBackView.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.8]; // Shadow above (all as above)
    newBackView.shadowOffset = shadowOffset;	
    [newBackView sizeToFit]; // Size to fit
    UIGraphicsBeginImageContext(newBackView.frame.size); // Start an image context the size of the view we want to render
    [newBackView.layer renderInContext:UIGraphicsGetCurrentContext()]; // Render the label we wanted to use into the image.  Note this gives compile time warning.  It is a documented call in the API...
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); // Get the image
    UIGraphicsEndImageContext(); // End the rendering
    [newBackView release]; // We can now release the view
    
    My warning is on the line with renderInContext: (as noted in the comments). I cannot understand why I am getting a warning. The layer property of UIView clearly returns a CALayer which has a renderInContext: methods.

    So, anyone got any ideas as to how to cleanly get rid of the warning (I don't consider that performSelector:withObject: is a clean solution really). Oh and sorry for the monster post!
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    You need to add QuartzCore.framework to your project and you need to import the

    #import <QuartzCore/CALayer.h>

    header into the correct file.
     
  3. robbieduncan thread starter Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Brilliant that fixed it thanks :) For some reason I assumed that if a method in UIKit returned a CALayer then CALayer.h would already be imported. I so much prefer code with no warnings.
     

Share This Page