UIImageView-Letters
UIImageView-Letters copied to clipboard
CGContextFillRects: invalid context 0x0
i'm getting this error on iOS 9 Swift
<Error>: CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Thanks @O-mkar. Do you have any relevant code or steps to reproduce?
@bachonk I found an alternative fix for the error till you fix this is how you produce the error
var imageView = UIImageView()
imageView.contentMode = .ScaleAspectFit
imageView.clipsToBounds = true
imageView.setImageWithString(name, color: nil, circular: false)
imageView.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
view.addSubview(imageView)
@O-mkar unfortunately, I'm still not seeing the issue on my end, but I'll continue to dig around. In the meantime, could you share what your alternative fix was?
when you set image in custom collection view cell, this issue will generated.
I am also getting the same error, i am setting background image to UIButton in custom collection view cell
The problem is using setImageWithString: when drawing context isn't ready. Try to move it into drawRect: or somewhere else, when image view's drawing context exists.
What I tried is to create a UIImageView through code as below and use it's image property when required (e.g. : cellForRowAtIndexpath for TableView or cellForItemAtIndexPath for CollectionView). And it worked well.
@property (nonatomic, retain) UIImageView *imgViewForAvatar;
_imgViewForAvatar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[_imgViewForAvatar setImageWithString:_@"Name Here" color:[_utils getThemeColor] circular:YES fontName:@"Lato-Regular"];
cellForRowAtIndexpath:
_chatCell.chatUserImage.image = _imgViewForAvatar.image;
@YahyaBagia I can confirm this works. It's hacky but it works.
I have my image view set up in the cell's init like so
let profileImageView = PFImageView() // PFImageView is just a Parse-based subclass of UIImageView
profileImageView.layer.cornerRadius = 25
profileImageView.clipsToBounds = true
profileImageView.isHidden = true
contentHolderView.addSubview(profileImageView)
constrain(profileImageView) {
$0.width == CGFloat(50)
$0.height == CGFloat(50)
$0.bottom == $0.superview!.bottom - CGFloat(2)
$0.left == $0.superview!.left + CGFloat(12)
}
self.profileImageView = profileImageView
Then in my view controller's cellForRowAtIndexPath:
let cell = tableView.dequeueReusableCell(withIdentifier: "GroupMessageCell") as! GroupMessageCell
img.setImage(withFile: nil, defaultText: message.senderUsername)
cell.profileImageView?.image = img.image
// other setup configuration
return cell
Hopefully this can help someone else out