Iphone 在coregraphics中创建的UIImage是';t重复

Iphone 在coregraphics中创建的UIImage是';t重复,iphone,objective-c,core-graphics,Iphone,Objective C,Core Graphics,我有这个代码,它应该重复相同的UIImage: UIView *paperMiddle = [[UIView alloc] initWithFrame:CGRectMake(0, 34, 320, rect.size.height - 34)]; UIImage *paperPattern = paperBackgroundPattern(context); paperMiddle.backgroundColor = [UIColor colorWithPatternImage:paperPat

我有这个代码,它应该重复相同的UIImage:

UIView *paperMiddle = [[UIView alloc] initWithFrame:CGRectMake(0, 34, 320, rect.size.height - 34)];
UIImage *paperPattern = paperBackgroundPattern(context);
paperMiddle.backgroundColor = [UIColor colorWithPatternImage:paperPattern];
[self addSubview:paperMiddle];
这是
平装背景模式
方法:

UIImage *paperBackgroundPattern(CGContextRef context) {
    CGRect paper3 = CGRectMake(10, -15, 300, 16);
    CGRect paper2 = CGRectMake(13, -15, 294, 16);
    CGRect paper1 = CGRectMake(16, -15, 288, 16);

    //Shadow
    CGContextSetShadowWithColor(context, CGSizeMake(0,0), 10, [[UIColor colorWithWhite:0 alpha:0.5]CGColor]);
    CGPathRef path = createRoundedRectForRect(paper3, 0);
    CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextAddPath(context, path);
    CGContextFillPath(context);

    //Layers of paper
    CGContextSaveGState(context);
    drawPaper(context, paper3);
    drawPaper(context, paper2);
    drawPaper(context, paper1);

    CGContextRestoreGState(context);
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 1), NO, 0);
    UIImage *paperImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return paperImage;
}
它没有重复图像。但是,图像显示在屏幕的顶部像素(这不是我给它的帧)


你知道为什么吗?

我不知道你要传递的
上下文是什么,但不管是什么,你都不应该卷入其中。而且您没有在使用
UIGraphicsBeginImageContextWithOptions
创建的上下文中绘制任何内容

如果要生成图像,不需要在上下文中传递,只需使用
UIGraphicsBeginImageContextWithOptions
为您制作的图像即可

UIImage *paperBackgroundPattern() {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 1), NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // draw into context, then...

    UIImage *paperImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return paperImage;
}

还有,你真的想制作一个320磅宽1英尺高的图像吗?你把这么精致的东西画成这么小的图片,这似乎很奇怪。

请不要删除并重新发布你的问题。改为编辑和改进。非常好,谢谢。图像有一个7.5像素大的内部阴影。所以它需要比这更大,以便它不显示从顶部或底部的阴影。我想我会删除它,因为我的代码正在更改为这个方法。