Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CGContextFillRects: invalid context 0x0 #20

Open
O-mkar opened this issue May 17, 2016 · 8 comments
Open

CGContextFillRects: invalid context 0x0 #20

O-mkar opened this issue May 17, 2016 · 8 comments

Comments

@O-mkar
Copy link

O-mkar commented May 17, 2016

i'm getting this error on iOS 9 Swift

: CGContextFillRects: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

@bachonk
Copy link
Owner

bachonk commented May 17, 2016

Thanks @O-mkar. Do you have any relevant code or steps to reproduce?

@O-mkar
Copy link
Author

O-mkar commented May 18, 2016

@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)

@bachonk
Copy link
Owner

bachonk commented Jun 3, 2016

@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?

@maheshcheliya
Copy link

when you set image in custom collection view cell, this issue will generated.

@ashusath
Copy link

ashusath commented Feb 6, 2017

I am also getting the same error, i am setting background image to UIButton in custom collection view cell

@mrublev
Copy link

mrublev commented Jul 26, 2017

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.

@YahyaBagia
Copy link

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;

@zackshapiro
Copy link

zackshapiro commented Sep 5, 2018

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants