gpt4 book ai didi

处理图像的 Iphone 停止所有进程

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:01 24 4
gpt4 key购买 nike

你好,我是 iPhone 开发的新手,所以我可能做错了。我想连续转换图像 3 次,但当我这样做时,它会锁定 iphone,直到它完成所有 3 次转换。我在步骤之间有功能,但在最后一个图像转换触发之前它们不会触发。如果您阅读下面的代码注释,这会更有意义。

我的问题是

  1. 有没有更快的方法来转换图像? 2. 我如何阻止它锁定,以便它按顺序触发代码并且图像之间的函数转换内联触发?

    - (IBAction)ColorFun1
    {
    //
    // ANY CODE IN THIS location will not fire until 3rd convert is finished
    //
    // Image to convert
    UIImage *originalImage = imageView.image;

    // 1st Convert

    CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, originalImage.size.width, originalImage.size.height, 8, originalImage.size.width, colorSapce, kCGImageAlphaNone);
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGContextSetShouldAntialias(context, NO);
    CGContextDrawImage(context, CGRectMake(0, 0, originalImage.size.width, originalImage.size.height), [originalImage CGImage]);
    CGImageRef bwImage = CGBitmapContextCreateImage(context);
    //
    CGContextRelease(context);
    CGColorSpaceRelease(colorSapce);
    //
    UIImage *resultImageBW = [UIImage imageWithCGImage:bwImage]; // This is result B/W image.
    [fxImage2View setImage:resultImageBW];



    //
    // ANY CODE IN THIS location will not fire until 3rd convert is finished

    //

    //
    //
    // 2nd Convert

    //

    UIGraphicsBeginImageContext(resultImageBW.size);

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);

    [resultImageBW drawInRect:CGRectMake(0, 0, resultImageBW.size.width, resultImageBW.size.height)];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);


    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor grayColor].CGColor);

    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, resultImageBW.size.width, resultImageBW.size.height));

    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
    [fxImage1View setImage:returnImage];
    UIGraphicsEndImageContext();

    //
    //

    //
    // ANY CODE IN THIS location will not fire until 3rd convert is finished
    //
    //

    //
    // 3rd Convert

    //

    UIGraphicsBeginImageContext(resultImageBW.size);

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);

    [resultImageBW drawInRect:CGRectMake(0, 0, resultImageBW.size.width, resultImageBW.size.height)];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeSoftLight);


    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor colorWithRed:40 green:20 blue:0 alpha:1].CGColor);

    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, resultImageBW.size.width, resultImageBW.size.height));

    returnImage = UIGraphicsGetImageFromCurrentImageContext();
    [fxImage3View setImage:returnImage];
    UIGraphicsEndImageContext();

    CGImageRelease(bwImage);

    }

最佳答案

您需要像 sergio 所说的那样将控制权转移回运行循环。我建议研究大中央调度。来自维基百科

Grand Central Dispatch still uses threads at the low level but abstracts them away from the programmer, who will not need to be concerned with as many details. Tasks in GCD are lightweight to create and queue; Apple states that 15 instructions are required to queue up a work unit in GCD, while creating a traditional thread could easily require several hundred instructions

找到有关如何实现它的教程应该不难。您甚至可以考虑斯坦福开发讲座。 This一个谈论性能和线程。我认为与您相关的部分从 33:36 开始。

关于处理图像的 Iphone 停止所有进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6899452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com