I have a custom UIView that draws an image in the draw rect method. My problem is UIView wants me to initWithFrame when I allocate the object. The frame of the view needs to be the same as the image size and width. The problem is that I don't get that image size and width until the drawRect method of the view I am creating. Inside the drawRect view changing the UIView's frame to the image dimensions seems to have no effect. So what I want to do is something like this:
MyCustomView *myCustomView = [[MyCustomView alloc] initWithFrame: CGrectMake(0,0,0,0)];
Then inside MyCustomView's drawRect method I want to somehow change the frame of the view to be the frame size of the image I am creating.
How do I get my desired outcome? An no, creating the image before I init my custom view and putting the size dimensions into the initWithFrame is not an option.
This is my current code in the view's drawRect method
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",self.imgName]];
CGSize imgSize = [image size];
CGRect newrect = CGRectMake(self.frame.origin.x, self.frame.origin.y, imgSize.width, imgSize.height);
self.frame = newrect; //This does nothing!!
CGRect imgRect = CGRectMake(0, 0, imgSize.width, imgSize.height);
[image drawInRect:imgRect];