裁切图像更快

问题描述 投票:0回答:2

我想我的作物在IOS映像一段时间了。该代码我的作品很好,但还不够快。当我与约20-25图像提供它需要7-10秒来处理它。我曾尝试各种可能的方式来解决这个问题,但都没有成功。我不知道我错过了什么。

- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)targetSize {

    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor > heightFactor)
        {
            scaleFactor = widthFactor; // scale to fit height
        }
        else
        {
            scaleFactor = heightFactor; // scale to fit width
        }

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else
        {
            if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }
    }

    UIGraphicsBeginImageContext(targetSize); // this will crop

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage == nil)
    {
        NSLog(@"could not scale image");
    }

    //pop the context to get back to the default
    UIGraphicsEndImageContext();

    return newImage;
}
ios core-graphics avfoundation
2个回答
4
投票

你原来的问题没有正确地说明问题,所以现在这样:这些操作需要很长时间的原因(不裁剪,这是更简单,更快)缩放图像所需的CPU周期数。缩放时,该系统需要使用一些数目包围的区域的像素,其消耗大量的CPU周期的混合。你可以让这个去使用的技术的组合更快,但没有一个统一的答案。

1)使用块和调度这些图像操作上并发调度队列,以获得并行性。我相信最新的iPad拥有4个核心,你可以把使用这种方式。 [UIGraphicsBeginImageContext是线程安全的。

2)获取ContextRef指针,并设置内插设定为最低设置:

UIGraphicsBeginImageContext(targetSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationLow);
...

3)作弊 - 不只是由两个大国规模。在这种技术中,你会确定的两个“最佳”电源通过缩小图像,展开的宽度和高度,以适应您的目标大小。如果你可以使用两个电源,可以从UIImage的使用CGImageRef,拿到像素的指针,并复制每隔像素/每隔一行,并真的很快创建一个更小的图像(使用CGImageCreate)。它可能不是为高品质,你会用让系统扩展您的图像得到的,但它会更快。这显然是一个代码相当多,但是可以使操作非常快这种方式。

4)重新定义你的任务。而不是试图调整一组图像,改变你的应用程序,让你一次只能显示一个或两个缩放后的图像,并且当用户在看他们,做一个后台队列中的其他图像操作。这是一个完整的,我假设你已经想到了这一点。

PS:如果这对你的作品,不需要赏金,帮助别人来代替。


1
投票

使用drawInRect是慢。你可以尝试CGImageCreateWithImageInRect这将是快(比drawInRect至少快10倍)。

CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], theRect); // e.g. theRect={{100,100, {200, 200}}

UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
© www.soinside.com 2019 - 2024. All rights reserved.