UIImageView-Letters icon indicating copy to clipboard operation
UIImageView-Letters copied to clipboard

CGContextFillRects: invalid context 0x0

Open O-mkar opened this issue 9 years ago • 8 comments

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.

O-mkar avatar May 17 '16 12:05 O-mkar

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

bachonk avatar May 17 '16 13:05 bachonk

@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 avatar May 18 '16 06:05 O-mkar

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

bachonk avatar Jun 03 '16 16:06 bachonk

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

maheshcheliya avatar Jun 24 '16 05:06 maheshcheliya

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

ashusath avatar Feb 06 '17 13:02 ashusath

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.

mrublev avatar Jul 26 '17 11:07 mrublev

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 avatar Feb 20 '18 07:02 YahyaBagia

@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

zackshapiro avatar Sep 05 '18 15:09 zackshapiro