0

我有一个使用 AVFoundation 的拍照应用程序。当用户按下按钮拍摄照片时,会发生很多事情,比如将照片旋转到正确的方向、裁剪等。

捕获图像时,我在控制台中收到“收到内存警告”消息,在我捕获了几张图像并删除它们后,应用程序将在 99% 的情况下崩溃。

我刚去用 Instruments 运行应用程序,调用树中有一种方法占用了 53.6% 的运行时间。我双击这个方法,显示了在这个方法的实现中每个语句花费的时间百分比,并且有一个语句占用了 90.5% 的时间。

显然,这是导致我的内存问题和应用程序崩溃的原因,但我不确定为什么或如何解决它。

下面是方法实现代码:

-(UIImage*) rotate:(UIImage*) src andOrientation:(UIImageOrientation)orientation

{
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context=(UIGraphicsGetCurrentContext());

      if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, 90/180*M_PI) ;
       } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, -90/180*M_PI);
       } else if (orientation == UIImageOrientationDown) {
        // NOTHING
       } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, 90/180*M_PI);
        }


    [src drawAtPoint:CGPointMake(0, 0)];



    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;

}

占用 90.5% 时间的语句是:

[src drawAtPoint:CGPointMake(0, 0)];

基本上,此方法实现的重点是将捕获的照片旋转到正确的方向,因为 iOS 默认情况下会将图像存储为横向,即使它只是以纵向捕获的。因此,您必须旋转回肖像才能获得它,就像它被捕获一样。

我不确定为什么上面的单个语句会导致问题,但我真的需要修复它,这样我才能提高我的应用程序的性能并阻止这些崩溃的发生。

4

0 回答 0