【发布时间】:2020-01-17 07:40:52
【问题描述】:
我正在处理图像。我有一个裁剪功能,它只允许以 2:1 的比例(宽:高比)进行裁剪。因此,如果图像是其他比例,用户可以缩小图像并将图像放在裁剪窗口内,但我想在图像的一侧添加黑色区域,以便生成的图像始终保持 2:1 的比例。
请看截图
这是我正在使用的代码:
#Check the ratio
#Get the resultant size ie. resultant width and height
//crashes here
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newImageWidth, newImageHeight), YES, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
CGPoint origin = CGPointMake((newImageWidth - image.size.width) / 2.0f, (newImageHeight - image.size.height) / 2.0f);
[image drawAtPoint:origin];
UIGraphicsPopContext();
此代码在 iOS 应用程序中运行良好,但当我在应用程序扩展中使用此代码时,由于内存泄漏而崩溃。它在 UIGraphicsBeginImageContextWithOptions 中崩溃。
有没有其他方法可以在图像中添加黑色区域而不会发生内存泄漏?
希望你能理解问题。
提前致谢。
【问题讨论】:
-
-[UIColor setFill]+UIRectFill -
@Cy-4AH 你能详细说明你的答案吗?
-
什么不清楚?设置填充黑色并填充您需要的矩形。
标签: ios objective-c uiimage ios-app-extension