xcode CGRectMake尺寸正在弄乱图像

时间:2014-06-30 17:39:20

标签: ios objective-c xcode

我有一张刮开的图像,我已经覆盖了第二张图像。导航项目的高度为63像素,我的页面底部有一个按钮,可以将用户引导到下一页。

当我绘制一个坐标为CGRectMake(0, 63, mainScreenwidth, mainScreenheight - 63)];的矩形时,图像显示完美,但它会覆盖页面底部的按钮。当我告诉高度为mainScreenheight - 126(距离底部63像素)时,图像会变得混乱,如下面的屏幕截图所示:

Zoomed out view of Image not drawing correctly

如果我然后将起始y坐标更改为CGRectMake(0, 100, mainScreenwidth, mainScreenheight - 63)];,那么乱搞的块高度为100而不是63.

下面是我的ViewController。

ScratchableView *scratchableView = [[ScratchableView alloc]initWithFrame:
    CGRectMake(0, 63, [[UIScreen mainScreen] applicationFrame].size.width, 
    [[UIScreen mainScreen] applicationFrame].size.height - 126)];

以下是我的观点。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CGSize newSize = CGSizeMake([[UIScreen mainScreen] applicationFrame].size.width, ([[UIScreen mainScreen] applicationFrame].size.height - 126));
        UIGraphicsBeginImageContext( newSize );
        [[UIImage imageNamed:@"scratchcover2.png"] drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        scratchable = newImage.CGImage;

        width = CGImageGetWidth(scratchable);
        height = CGImageGetHeight(scratchable);

        self.opaque = NO;
        CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();

        CFMutableDataRef pixels = CFDataCreateMutable( NULL , width * height );
        alphaPixels = CGBitmapContextCreate(CFDataGetMutableBytePtr( pixels )  , width , height , 8 , width , colorspace , (CGBitmapInfo)kCGImageAlphaNone );
        provider = CGDataProviderCreateWithCFData(pixels);


        CGContextSetFillColorWithColor(alphaPixels, [UIColor blackColor].CGColor);
        CGContextFillRect(alphaPixels, frame);

        CGContextSetStrokeColorWithColor(alphaPixels, [UIColor whiteColor].CGColor);
        CGContextSetLineWidth(alphaPixels, 20.0);
        CGContextSetLineCap(alphaPixels, kCGLineCapRound);

        CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, width, provider, nil, NO);

        scratched = CGImageCreateWithMask(scratchable, mask);

        CGImageRelease(mask);
        CGColorSpaceRelease(colorspace);
    }
    return self;
}

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

CGRectMake的{​​{1}}弄乱了图像。我能找到的唯一解决方案是将CGFloat y值设置为CGFloat y并通过将多个像素添加到顶部来更改图像。

自:

0

要:

CGRectMake(0, 63, [[UIScreen mainScreen] applicationFrame].size.width, 
[[UIScreen mainScreen] applicationFrame].size.height - 126)];