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

Loads of "invalid context 0x0" #1203

Closed
cytryn opened this issue Jul 3, 2015 · 41 comments
Closed

Loads of "invalid context 0x0" #1203

cytryn opened this issue Jul 3, 2015 · 41 comments
Milestone

Comments

@cytryn
Copy link

cytryn commented Jul 3, 2015

Hi, when I implemented SDWebImage I got many and many of this error:

<Error>: CGContextScaleCTM: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

I am loading images to set as the background of my UITableViewCells on my cellForRowAtIndexPath:

[cell.cityImageView sd_setImageWithURL:imageURL placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
    NSLog(@"error: %@", error);
}];

Reading some stackoverflow topics I have found that some people said that this is an iOS 7 problem that would eventually disappear. But I am using iOS 8 simulator and the error is still there.

@mythodeia
Copy link
Contributor

can you check the url of the images that produce this console warning? find one and load it in your browser. does it load? please share a url

@mythodeia
Copy link
Contributor

@cytryn any updates

@cytryn
Copy link
Author

cytryn commented Jul 6, 2015

This is an example of URL:

https://res.cloudinary.com/saidacerta-staging/image/upload/v1433442235/cities/1/pictures/6b60dc6841e44a6a8ce4d7f6246088ae

It load perfectly on browser and on the app also...

@mythodeia
Copy link
Contributor

@cytryn can you try your image on the demo project. i just tried it in the simulator and got no warnings at all.

@cytryn
Copy link
Author

cytryn commented Jul 6, 2015

Just tested and still get the same errors.

@mythodeia have you tried using the same code block?

@cytryn
Copy link
Author

cytryn commented Jul 6, 2015

@mythodeia actually I have just downloaded the demo project and cannot run because I am getting some errors:

clang: error: no such file or directory: '/Users/acytryn/Downloads/iOS/SDWebImage-master/Vendors/libwebp/src/dsp/upsampling_sse2.c'
clang: error: no input files

@mythodeia
Copy link
Contributor

you need to clone the repo
git clone --recursive https://github.com/rs/SDWebImage.git

then do a pod install in the tests folder.
then open the xcworkspace file

then remove all sample urls and add only yours and debug

@cytryn
Copy link
Author

cytryn commented Jul 6, 2015

Did it, in the example it is doing fine, no warnings even with the code block that I used in my code.

I just tested in my code passing nil to the imageURL, I still get tons of warnings...

But if I remove the image download block I get no warnings at all.... so it is pretty much this code block that is causing the warnings, but why?

@mythodeia
Copy link
Contributor

are you using the latest code from this library?

EDIT:
by the way...why are you passing nil in the sd_setImageWithURL as you said in your post above?
if you pass nil you are actually calling the method with a nil URL, which will not load anything.

@cytryn use this:

    __weak __typeof(self)wself = self;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]
                  placeholderImage:nil
                         completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                             if(image) {
                                 [wself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
                             }
                         }];

@livyathan
Copy link

I'm having a similar issue. I get this error messages:

: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 16 bits/pixel; 1-component color space; kCGImageAlphaPremultipliedFirst; 224 bytes/row.
Jul 16 10:41:06 livyathans-iPhone SDWebImage Demo[483] : CGContextDrawImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
Jul 16 10:41:06 livyathans-iPhone SDWebImage Demo[483] : CGBitmapContextCreateImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

It appears than when this error is triggered, the image isn't displayed. I tried to isolate the problem by using the Demo example, and I think I found something. I can consistently reproduce this error with images with "Color space: Gray".

Here are 3 examples you can use:

https://danbooru.donmai.us/data/preview/1a3183f5ee226e8af06d8faf0244e079.jpg
https://danbooru.donmai.us/data/preview/d3f8f9d294678bba6d91e89a3bdd054f.jpg
https://danbooru.donmai.us/data/preview/f18c863d7024277b054a1f05c60bb3ba.jpg

Let me know if you need more examples.

Edit: I forgot to mention, I'm working with an iPhone 6, iOS 8.4, Xcode 6.4, iOS SDK 8.4 and target iOS 8.0

@mythodeia
Copy link
Contributor

@livyathan i will take a look at it later on. thanks for reporting it

@mythodeia
Copy link
Contributor

@livyathan
temporary solution:
go into SDWebImageDecoder.m and leave only this kCGBitmapByteOrderDefault on line 37

@mythodeia
Copy link
Contributor

from a bit more research those images have kCGColorSpaceGenericGray which is unavailable on IOS CG_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_NA);

i will try and implement a solution for these kind of images but until then you can remove the bitmap info i told you above and it will show the images

@livyathan
Copy link

I tried the solution, and it works, but only for those types of images, the normal images (with color space RGB) now throw a similar error, even the images from the demo will throw an error.

I didn't noticed this error until I updated the library with the latest version today, I was using a 2-3 month old version, so I related the problem to the library, and not the sources of the images. I don't know much about this, but if you say that these images cannot be decoded, at least consider adding a validation.

I hope you can find a better solution. Thank you.

@mythodeia
Copy link
Contributor

@livyathan can you replace the whole method decodedImageWithImage
with the one below?
i am close to a final solution but please try this one and let me know

+ (UIImage *)decodedImageWithImage:(UIImage *)image {
    // do not decode animated images
    if (image.images) { return image; }

    CGImageRef imageRef = image.CGImage;

    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
    BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
                     alpha == kCGImageAlphaLast ||
                     alpha == kCGImageAlphaPremultipliedFirst ||
                     alpha == kCGImageAlphaPremultipliedLast);

    if (anyAlpha) { return image; }

    size_t width = CGImageGetWidth(imageRef);
    size_t height = CGImageGetHeight(imageRef);

    // default RGB
    CGColorSpaceRef RGBcolorSpace = CGColorSpaceCreateDeviceRGB();

    // current
    //CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));

    CGContextRef context = CGBitmapContextCreate(NULL, width,
                                                 height,
                                                 CGImageGetBitsPerComponent(imageRef),
                                                 0,
                                                 RGBcolorSpace,
                                                 kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

    // Draw the image into the context and retrieve the new image, which will now have an alpha layer
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGImageRef imageRefWithAlpha = CGBitmapContextCreateImage(context);
    UIImage *imageWithAlpha = [UIImage imageWithCGImage:imageRefWithAlpha];

    CGColorSpaceRelease(RGBcolorSpace);
    CGContextRelease(context);
    CGImageRelease(imageRefWithAlpha);

    return imageWithAlpha;
}

@livyathan
Copy link

@mythodeia I just tried the code on the demo app and it seems to be working fine, I can see every image without errors. I'm using a mixture of RGB and Gray images at the same time.

I can test the code on my app but that will take a little longer, I'll try to report back tomorrow.

@mythodeia
Copy link
Contributor

@livyathan thanks
Please let me know if it gets the job done :)

@livyathan
Copy link

@mythodeia I tested the changes on my app today and it's working great, I cannot reproduce the error anymore. I hope you can add the changes to the main branch

Again, thank you for the help.

@cytryn
Copy link
Author

cytryn commented Jul 19, 2015

Great!

@rnewman
Copy link

rnewman commented Jul 30, 2015

Should this be reopened until the fix is applied?

@mythodeia
Copy link
Contributor

PR merged

@bpoplauschi bpoplauschi added this to the 4.0.0 milestone Jul 31, 2015
@marcomempin
Copy link

@mythodeia I've tried the code you posted here and it removes the warnings. Thanks!!!

@mythodeia
Copy link
Contributor

@Takdol check #1236
as it has some updated code

@marcomempin
Copy link

@mythodeia still getting the errors. Asked my colleague and he told me that our images are using SRGB color space

@mythodeia
Copy link
Contributor

with which version are you getting errors? if neither the code in this post above nor the code in this pull request #1236 fixes it for you then please share an image so i can test

@marcomempin
Copy link

Can't seem to find the version of the repo, but I cloned it 4 days ago. Will update the build and get back to you

@marcomempin
Copy link

@mythodeia updated to the latest commit, still having the issues.

try this url:
https://static.spuul.com/uploads/video/horizontal_cover/1928/small_6589ee9d42_5e4c053111_special26_banner.jpg

@mythodeia
Copy link
Contributor

@Takdol
can you try that url in the demo project? i just tried it and i can see the thumbnail just fine
please use the latest commit.

@marcomempin
Copy link

Ok will do.

@marcomempin
Copy link

hey @mythodeia, I've tried the url in the demo project and no problems there. It's working fine now. THANKS!

@mythodeia
Copy link
Contributor

@Takdol 👍

@ayakunin
Copy link

Hello, I have a same problem with with image http://alfa-dev.allychat.ru/static/upload/cf2ea3bf-3f29-4c93-a063-ffa686ca4445.png

@mythodeia
Copy link
Contributor

open your SDWebImageDecoder.m and replace this [line 38]:

   CGContextRef context = CGBitmapContextCreate(NULL, width,
                                                 height,
                                                 CGImageGetBitsPerComponent(imageRef),
                                                 0,
                                                 (imageColorSpaceModel == 0 || imageColorSpaceModel == -1) ? RGBcolorSpace : CGImageGetColorSpace(imageRef),
                                                 kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

with this

    CGContextRef context = CGBitmapContextCreate(NULL, width,
                                                 height,
                                                 CGImageGetBitsPerComponent(imageRef),
                                                 0,
                                                (imageColorSpaceModel == 0 || imageColorSpaceModel == -1 || imageColorSpaceModel == kCGColorSpaceModelIndexed) ? RGBcolorSpace : CGImageGetColorSpace(imageRef),
                                                 kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

@ayakunin
Copy link

Thx for answer, it solved my problem,

@A1314
Copy link

A1314 commented Sep 24, 2015

I test with new SDWebImage like top show , but some error also exit . For example :: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 40 bits/pixel; 4-component color space; kCGImageAlphaPremultipliedFirst; 1024 bytes/row.
: CGContextDrawImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
CGBitmapContextCreateImage: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
and so on...
what can I do ? Please help me ,Thanks very much!

@mythodeia
Copy link
Contributor

@A1314 if you put your urls in the demo project do you have the same issue? Please share the urls after you tried with the demo project and had the same issues

@A1314
Copy link

A1314 commented Sep 24, 2015

@mythodeia my url is " http://joke.luckyamy.com/api/?cat=pic&p=2&ap=joke&ver=1.7" , but I don't know which's the picture

@A1314
Copy link

A1314 commented Sep 24, 2015

@mythodeia Thanks

@mythodeia
Copy link
Contributor

@A1314 i tried the urls in the json export you provided (the thumbnails only) and none give me the warning of 'invalid context...'. Please if you find the url try it on the demo project first and then share it here

by the way you can use this to add the raw json and produce a readable text so you can extract the urls

@A1314
Copy link

A1314 commented Sep 24, 2015

ok,I test angain.of course ,if I find this problem in the future,I will share it here first.

@mythodeia
Copy link
Contributor

👍

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

8 participants