CGSize cgs = CGSizeMake(250.0, 300.0);
UIGraphicsBeginImageContext(cgs);
CGRect rectangle = CGRectMake(0,0,cgs.width,cgs.height);
UIImage *myImage = [UIImage imageNamed:@"BMW.jpg"];
[myImage drawInRect:rectangle];
[myImage release];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextStrokeRect(context, rectangle);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[testImg drawAtPoint:CGPointMake(35, 10)];
我想在图像和绘制图像的矩形边框之间留出间隙,比如左边 5 个,顶部 5 个,右边 5 个,底部大约 30 个,因为它是用户将要书写的文本空间,用户也可以选择他/她想要文本在顶部,所以在这种情况下,顶部间隙将为 30,底部间隙将为 5。这是 drawrect 方法中 uiview 子类中的代码。
使用此代码边框和图像紧密结合在一起,我无法给出边框和图像之间的间隙。知道我该怎么做吗?
提前致谢。
最佳答案
首先,您有一个严重的错误。这一行是错误的:
[myImage release];
您不拥有 myImage
,因此您不应该 发布它。您需要阅读 Cocoa Core Competencies 中的“内存管理规则” .或者你应该打开 ARC(自动引用计数),然后编译器会负责为你释放东西。由于自 iOS 4.0 起就支持 ARC,因此如果您正在开发新应用,您几乎肯定会使用它。
关于你的图像绘制,你只需要修改你绘制图像的矩形。例如,您可以在顶部、左侧和右侧保留 5 像素的边框,在底部保留 30 像素的边框,如下所示:
CGRect imageRect = CGRectInset(rectangle, 5, 5); // move 5 points in on all sides
imageRect.size.height -= 25; // make it another 25 points shorter
[myImage drawInRect:imageRect];
如果想要顶部的30点边框,还需要修改imageRect.origin.y
。
关于ios - 使用 quartz2d 进行图像处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9323586/