colorWithPatternImage creates black grids in iphone 4

前端 未结 2 2071
迷失自我
迷失自我 2021-01-16 08:58

I have

UIView *topPart = [[UIView alloc] initWithFrame:CGRectMake(9, 0, 302, 318)];
topPart.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageN         


        
相关标签:
2条回答
  • 2021-01-16 09:15

    I figured it out by combining the answers from these 2 posts https://devforums.apple.com/message/259150#259150

    How can I use CGContextDrawTiledImage to tile an image?

    UIImage *thePattern= [self GetFineTiles:[UIImage imageNamed:@"Feedbackpattern.png"]];
    theView.backgroundColor = [UIColor colorWithPatternImage:thePattern];
    
    -(UIImage*)GetFineTiles:(UIImage*)badImage{
        UIGraphicsBeginImageContext(badImage.size);
        CGContextRef imageContext = UIGraphicsGetCurrentContext();
        CGContextDrawTiledImage(imageContext, (CGRect){CGPointZero,badImage.size}, badImage.CGImage);
        UIImage *goodPattern = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return goodPattern;
    }
    

    Hope this helps others

    0 讨论(0)
  • 2021-01-16 09:27

    I was experiencing the same issue and found it was due to my png being greyscale (not RGB)

    To check if this is the case, just select the image in Xcode, show the Utilities sidebar (the one that appears from the right) and look at the Color Space.

    To fix this you can use your favourite image editor, but for photoshop do this: Open the greyscale image Select all and copy Create a new document (but this time make sure the colour space is RGB) Paste

    Hope it helps :)

    0 讨论(0)
提交回复
热议问题