IOS Objective C 在视图上绘制位图

IOS Objective C Draw Bitmap on View

我正在使用以下内容在 Drawrect 中的视图上显示 jpg,如下所示

- (void)drawRect:(CGRect)rect
{

UIImage *anImage2 = [UIImage imageNamed:@"imagetest.jpg"];
CGRect imageFrame = CGRectMake(440,140, 140, 187);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:imageFrame cornerRadius:30].CGPath;
CGContextAddPath(ctx, clippath);
CGContextClip(ctx);
[anImage2 drawInRect:imageFrame];
CGContextRestoreGState(ctx);
}

这很完美,但是我需要从沙盒中加载图像。如果我将代码更改为以下

- (void)drawRect:(CGRect)rect
{
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@", docDir, @"imagetest.jpg"];    
UIImage *anImage2 = [UIImage imageNamed: pngFilePath];
CGRect imageFrame = CGRectMake(440,140, 140, 187);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGPathRef clippath = [UIBezierPath bezierPathWithRoundedRect:imageFrame cornerRadius:30].CGPath;
CGContextAddPath(ctx, clippath);
CGContextClip(ctx);
[anImage2 drawInRect:imageFrame];
CGContextRestoreGState(ctx);
}

现在没有任何显示

我已经检查过,图片路径正确,文件存在,并且与捆绑包中的 jpg 相同,所以我不明白哪里出错了

有什么想法吗?

马克

[UIImage imageNamed] 不接受图像路径,只接受图像名称。尝试 [UIImage imageWithContentsOfFile:]。 Apple 的文档说明了该方法 "creates and returns an image object by loading the image data from the file at the specified path."