草庐IT

iphone - slider 变化时模糊 UIImage

coder 2023-09-21 原文

我已经尝试过高斯模糊并检查了 stackoverflow 上的所有问题,但没有一个解决了我的崩溃问题。请帮助除了高斯模糊算法之外还有其他方法可以模糊图像。我的图像大小是 768x1024,循环迭代了 2*1024*768 次,这是不可行的。

CGContextRef NYXImageCreateARGBBitmapContext(const size_t width, const size_t height, const size_t bytesPerRow)
{
/// Use the generic RGB color space
/// We avoid the NULL check because CGColorSpaceRelease() NULL check the value anyway, and worst case scenario = fail to create context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

/// Create the bitmap context, we want pre-multiplied ARGB, 8-bits per component
CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8/*Bits per component*/, bytesPerRow, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

return bmContext;
}



 -(UIImage*)blurredImageUsingGaussFactor:(NSUInteger)gaussFactor andPixelRadius:(NSUInteger)pixelRadius
{
CGImageRef cgImage = self.CGImage;
const size_t originalWidth = CGImageGetWidth(cgImage);
const size_t originalHeight = CGImageGetHeight(cgImage);
const size_t bytesPerRow = originalWidth * 4;
CGContextRef context = NYXImageCreateARGBBitmapContext(originalWidth, originalHeight, bytesPerRow);
if (!context) 
    return nil;

unsigned char *srcData, *destData, *finalData;

size_t width = CGBitmapContextGetWidth(context);
size_t height = CGBitmapContextGetHeight(context);
size_t bpr = CGBitmapContextGetBytesPerRow(context);
size_t bpp = CGBitmapContextGetBitsPerPixel(context) / 8;
CGRect rect = {{0.0f, 0.0f}, {width, height}}; 

CGContextDrawImage(context, rect, cgImage); 

// Now we can get a pointer to the image data associated with the bitmap
// context.
srcData = (unsigned char*)CGBitmapContextGetData(context);
if (srcData != NULL)
{
    size_t dataSize = bpr * height;
    finalData = malloc(dataSize);
    destData = malloc(dataSize);
    memcpy(finalData, srcData, dataSize);
    memcpy(destData, srcData, dataSize);

    int sums[gaussFactor];
    size_t i, /*x, y,*/ k;
    int gauss_sum = 0;
    size_t radius = pixelRadius * 2 + 1;
    int *gauss_fact = malloc(radius * sizeof(int));

    for (i = 0; i < pixelRadius; i++)
    {
        gauss_fact[i] = 1 + (gaussFactor * i);
        gauss_fact[radius - (i + 1)] = 1 + (gaussFactor * i);
        gauss_sum += (gauss_fact[i] + gauss_fact[radius - (i + 1)]);
    }
    gauss_fact[(radius - 1) / 2] = 1 + (gaussFactor*pixelRadius);
    gauss_sum += gauss_fact[(radius - 1) / 2];

    unsigned char *p1, *p2, *p3;

    for (size_t y = 0; y < height; y++) 
    {
        for (size_t x = 0; x < width; x++) 
        {
            p1 = srcData + bpp * (y * width + x); 
            p2 = destData + bpp * (y * width + x);

            for (i = 0; i < gaussFactor; i++)
                sums[i] = 0;

            for (k = 0; k < radius ; k++)
            {
                if ((y - ((radius - 1) >> 1) + k) < height)
                    p1 = srcData + bpp * ((y - ((radius - 1) >> 1) + k) * width + x); 
                else
                    p1 = srcData + bpp * (y * width + x);

                for (i = 0; i < bpp; i++)
                    sums[i] += p1[i] * gauss_fact[k];

            }
            for (i = 0; i < bpp; i++)
                p2[i] = sums[i] / gauss_sum;
        }
    }
    for (size_t y = 0; y < height; y++) 
    {
        for (size_t x = 0; x < width; x++) 
        {
            p2 = destData + bpp * (y * width + x);
            p3 = finalData + bpp * (y * width + x);

            for (i = 0; i < gaussFactor; i++)
                sums[i] = 0;

            for(k = 0; k < radius ; k++)
            {
                if ((x - ((radius - 1) >> 1) + k) < width)
                    p1 = srcData + bpp * ( y * width + (x - ((radius - 1) >> 1) + k)); 
                else
                    p1 = srcData + bpp * (y * width + x);

                for (i = 0; i < bpp; i++)
                    sums[i] += p2[i] * gauss_fact[k];

            }
            for (i = 0; i < bpp; i++)
            {
                p3[i] = sums[i] / gauss_sum;
            }
        }
    }
}

size_t bitmapByteCount = bpr * height;

///////Here was the problem.. you had given srcData instead of destData.. Rest all 
//were perfect...
CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, destData, bitmapByteCount, NULL);

CGImageRef blurredImageRef = CGImageCreate(width, height, CGBitmapContextGetBitsPerComponent(context), CGBitmapContextGetBitsPerPixel(context), CGBitmapContextGetBytesPerRow(context), CGBitmapContextGetColorSpace(context), CGBitmapContextGetBitmapInfo(context), dataProvider, NULL, true, kCGRenderingIntentDefault);

CGDataProviderRelease(dataProvider);
CGContextRelease(context); 
if (destData)
    free(destData);
if (finalData)
    free(finalData);

UIImage* retUIImage = [UIImage imageWithCGImage:blurredImageRef];

CGImageRelease(blurredImageRef);

return retUIImage;
}

最佳答案

我对 UIImage 做了一个小的 StackBlur 扩展。 StackBlur 接近于 GaussianBlur,但速度更快。

查看:https://github.com/tomsoft1/StackBluriOS


小记...那个自述文件上有一个错字,“normalized”是“normalize”

关于iphone - slider 变化时模糊 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8758433/

有关iphone - slider 变化时模糊 UIImage的更多相关文章

  1. ruby-on-rails - 启用 Rack::Deflater 时 ETag 发生变化 - 2

    在启用Rack::Deflater来gzip我的响应主体时偶然发现了一些奇怪的东西。也许我遗漏了一些东西,但启用此功能后,响应被压缩,但是资源的ETag在每个请求上都会发生变化。这会强制应用程序每次都响应,而不是发送304。这在没有启用Rack::Deflater的情况下有效,我已经验证页面源没有改变。我正在运行一个使用thin作为Web服务器的Rails应用程序。Gemfile.lockhttps://gist.github.com/2510816有没有什么方法可以让我从Rack中间件获得更多的输出,这样我就可以看到发生了什么?提前致谢。 最佳答案

  2. ruby-on-rails - ruby 中两个哈希之间的变化 - 2

    我有两个具有以下格式的哈希mydetails[x['Id']]=x['Amount']这将包含如下数据hash1={"A"=>"0","B"=>"1","C"=>"0","F"=>"1"}hash2={"A"=>"0","B"=>"3","C"=>"0","E"=>"1"}我期待这样的输出:Differencesinhash:"B,F,E"非常感谢任何帮助。 最佳答案 这个解决方案可能更容易理解:(hash1.keys|hash2.keys).select{|key|hash1[key]!=hash2[key]}Array#|返回2

  3. ruby-on-rails - 使用 Rails (Tire) 和 ElasticSearch 进行模糊字符串匹配 - 2

    我有一个Rails应用程序,现在设置了ElasticSearch和Tiregem以在模型上进行搜索,我想知道我应该如何设置我的应用程序以对模型中的某些索引进行模糊字符串匹配。我将我的模型设置为索引标题、描述等内容,但我想对其中一些进行模糊字符串匹配,但我不确定在何处进行此操作。如果您想发表评论,我将在下面包含我的代码!谢谢!在Controller中:defsearch@resource=Resource.search(params[:q],:page=>(params[:page]||1),:per_page=>15,load:true)end在模型中:classResource'Us

  4. Ruby:模糊测试所有 unicode 字符(UTF8/编码/字符串操作) - 2

    我无法遍历整个unicode字符范围。我到处找...我正在构建一个模糊器,并希望将所有unicode字符(一次一个)嵌入到一个url中。例如:http://www.example.com?a=\uff1c我知道有一些内置工具,但我需要更多的灵active。如果我能像下面那样做:"\u"+"ff1c"那就太好了。这是我得到的最接近的:char="\u0000"...#withiniterationchar.succ!...但在字符"\u0039"之后,即数字9,我将得到"10"而不是":" 最佳答案 您可以使用pack将数字转换为UT

  5. ruby-on-rails - 使用 Barby gem 打印 - 条码图像模糊 - 2

    有人使用Barbygem(https://github.com/toretore/barby)来生成和打印条形码吗?我正在生成和打印png条形码文件,但它们变得模糊不清。请注意,生成的PNG文件看起来很棒,即使在放大条形图时也是如此,但打印时边缘模糊。我正在使用同一台打印机打印与我在其他地方获得的相同尺寸和格式的条形码,它们看起来漂亮而清晰。这是我正在使用的代码,以防有人对我如何让它看起来更好有任何想法。它目前无法使用,因为扫描仪无法读取它。我注意到创建的png有72dpi,似乎没有办法让它更高。这可能与此有关,也可能无关。require'barby'require'barby/bar

  6. ruby-on-rails - Rails 3.2.11 突然需要重启到 'acknowledge' Controller 有什么变化吗? - 2

    标题说明了一切。请注意,这不是模型或初始值设定项的更改。我可以删除Controller中的一个实例变量(例如,@user),然后重新加载一个View,它会工作-直到我重新启动服务器,在这种情况下它会提示变量为nil。我正常工作,然后切换到一组完全不同的Controller和View上工作,现在它无缘无故地发生了。应用处于开发环境中。development.rb内容:Dashboard::Application.configuredoconfig.cache_classes=falseconfig.whiny_nils=trueconfig.consider_all_requests_l

  7. iphone - 扩展 restful_authentication/AuthLogic 以支持匿名 iPhone 的延迟登录的最佳方法是什么? - 2

    我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符

  8. PLUS模型和InVEST模型生态系统服务多情景模拟预测、ArcGIS空间数据处理、空间分析与制图、土地利用时空变化 - 2

    查看原文>>>基于”PLUS模型+“生态系统服务多情景模拟预测实践技术应用目录第一章、理论基础与软件讲解第二章、数据获取与制备第三章、土地利用格局模拟第四章、生态系统服务评估第五章、时空变化及驱动机制分析第六章、论文撰写技巧及案例分析基于ArcGISPro、Python、USLE、INVEST模型等多技术融合的生态系统服务构建生态安全格局基于生态系统服务(InVEST模型)的人类活动、重大工程生态成效评估、论文写作等具体应用基于ArcGISPro、R、INVEST等多技术融合下生态系统服务权衡与协同动态分析实践应用    本文从数据、方法、实践三方面对生态系统服务多情景预测进行讲解。内容涵盖多

  9. ruby - 缓慢的 Ruby 正则表达式通过奇怪的变化变得快速 - 2

    我一直在调试网站以查找页面加载时间过长的根源,并将其缩小为用于从文本中提取URL的正则表达式:/(?:([\w+.-]+):\/\/|(?:www\.))[^\s在一大块文本上运行大约需要3秒。我发现如果我将第一个子句的逆语句添加到正则表达式((?:[^\w+.-]|^))的开头,它几乎会立即运行:/(?:[^\w+.-]|^)(?:([\w+.-]++):\/\/|(?:www\.))[^\s在我看来,添加的子句根本不应该影响正则表达式,因为没有什么可以导致该子句失败(因为这些字符将与“[\w+.-]++”子句匹配)。为什么这会使正则表达式运行得更快?编辑有些人要求提供我正在尝试做的

  10. ruby-on-rails - 仅当可搜索字段发生变化时才使用太阳黑子索引 - 2

    我们在最新项目中使用sunspot进行搜索。我们还使用devise并按如下方式为我们的用户模型编制索引:searchabledotext:fnametext:lnametext:emailtext:descriptiontext:twitter_usernameend使用此设置,除非solr正在运行,否则用户甚至无法登录。这意味着在每次保存用户模型时,都会与我们的solr服务器进行一些通信(重建索引?),即使可搜索字段都没有更改。这是正确的吗?我们还有许多其他模型正在被sunspot索引,这些模型具有经常更新的不可搜索字段。似乎sunspot正在为所有这些更新重新编制索引。有没有办法将

随机推荐