我有两种方法正在使用,但它们的效果并不理想。第一个是 Perlin 噪声发生器,它导出随机云的黑白 UIImage,并且运行良好。第二种方法采用 UIImage 并过滤掉高于或低于给定亮度的所有像素,在不需要的像素所在的位置返回具有透明度的图像,并且它与我一直使用的黑白测试图像完美配合。
但是当我尝试将图像从第一种方法输入到第二种方法时,它不起作用。无论输入值如何,每个像素都被删除,我得到一个空白的 UIImage。 (需要明确的是,这是一个非零的 UIImage,除了透明像素外什么都没有,就好像每个像素都被计为超出所需的亮度范围,而不管该像素的实际亮度如何。)
下面是两种方法。我从教程和 SO 答案中改编了每一个,但虽然我对 Core Graphics 不是 100% 满意,但它们对我来说似乎相当简单:第一个遍历每个像素并使用 Perlin 公式中的 RGB 值为其着色,第二个创建基于输入值的掩码。 (注意:两者都是 UIImage 上的类别方法,因此后一种方法中的“self”引用指的是源图像。)
+ (UIImage *)perlinMapOfSize:(CGSize)size {
CZGPerlinGenerator *generator = [[CZGPerlinGenerator alloc] init];
generator.octaves = 10;
generator.persistence = 0.5;
generator.zoom = 150;
CGContextRef ctx = [self contextSetup:size];
CGContextSetRGBFillColor(ctx, 0.000, 0.000, 0.000, 1.000);
CGContextFillRect(ctx, CGRectMake(0.0, 0.0, size.width, size.height));
for (CGFloat x = 0.0; x<size.width; x+=1.0) {
for (CGFloat y=0.0; y<size.height; y+=1.0) {
double value = [generator perlinNoiseX:x y:y z:0 t:0];
CGContextSetRGBFillColor(ctx, value, value, value, 1.0);
CGContextFillRect(ctx, CGRectMake(x, y, 1.0, 1.0));
}
}
return [self finishImageContext];
}
-(UIImage*)imageWithLumaMaskFromDark:(CGFloat)lumaFloor toLight:(CGFloat)lumaCeil {
// inputs range from 0 - 255
CGImageRef rawImageRef = self.CGImage;
const CGFloat colorMasking[6] = {lumaFloor, lumaCeil, lumaFloor, lumaCeil, lumaFloor, lumaCeil};
UIGraphicsBeginImageContext(self.size);
CGImageRef maskedImageRef = CGImageCreateWithMaskingColors(rawImageRef, colorMasking);
{
//if in iphone
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, self.size.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
}
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height), maskedImageRef);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
CGImageRelease(maskedImageRef);
UIGraphicsEndImageContext();
return result;
}
有谁知道为什么前一种方法的图像与后者不兼容?前一种方法成功返回云图像,后一种方法处理我从计算机或互联网输入的每张图像,而不是前一种方法的图像。
我假设第二种方法中的 CGImageCreateWithMaskingColors() 调用正在寻找第一种方法没有放入图像中的一些信息,或者其他我不知道的东西系统足够好,可以找出问题所在。
任何人都可以解释一下吗?
编辑:根据要求,这里是上面提到的另外两种方法。我知道,在类别中使用类似这样的类方法是一种奇怪的设置,但这是我在教程中找到代码的方式并且它有效,所以我从来没有费心去改变它。
+ (CGContextRef) contextSetup: (CGSize) size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
//NSLog(@"Begin drawing");
return context;
}
+ (UIImage *) finishImageContext {
//NSLog(@"End drawing");
UIGraphicsPopContext();
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outputImage;
}
编辑 2: 根据一些研究,CGImageCreateWithMaskingColors() 函数不适用于包含 alpha 分量的图像,我重新安排了第一种方法,如下所示.我的直觉告诉我这就是问题所在,但我有点摸不着头脑。这是我尝试使用 kCGImageAlphaNone 创建图像的尝试,但现在最后的 UIGraphicsGetImageFromCurrentImageContext() 返回 nil。
+ (UIImage *)perlinMapOfSize:(CGSize)size {
CZGPerlinGenerator *generator = [[CZGPerlinGenerator alloc] init];
generator.octaves = 10;
generator.persistence = 0.5;
generator.zoom = 150;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width * 4, colorSpace, kCGImageAlphaNoneSkipLast);
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, 0.000, 0.000, 0.000, 1.0);
CGContextFillRect(ctx, CGRectMake(0.0, 0.0, size.width, size.height));
for (int x = 0; x<size.width; x++) {
for (int y=0; y<size.height; y++) {
double value = [generator perlinNoiseX:x y:y z:0 t:0];
CGContextSetRGBFillColor(ctx, value, value, value, 1.0);
CGContextFillRect(ctx, CGRectMake(x, y, 1.0, 1.0));
}
}
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"Output: %@", outputImage);
UIGraphicsEndImageContext();
CGContextRelease(ctx);
return outputImage;
}
最佳答案
正如您所发现的,CGImageCreateWithMaskingColors 需要没有 alpha channel 的图像。但是,您尝试的修复不起作用 @matt points out因为您正在尝试将图像上下文函数调用(例如 UIGraphicsGetImageFromCurrentImageContext)与位图上下文混合和匹配。
因此,最简单的解决方法是继续使用图像上下文,但将其设置为不透明。您可以通过调用 UIGraphicsBeginImageContextWithOptions 并将 YES 提供给 opaque 参数来执行此操作,这将输出没有 alpha channel 的图像。
尽管如此,在这里使用位图上下文将是更合适的解决方案,因为它允许您直接操作像素数据,而不是进行大量的 CGContextFillRect 调用。
像这样应该可以达到预期的效果:
+(UIImage *)perlinMapOfSize:(CGSize)size {
// your generator setup
CZGPerlinGenerator *generator = [[CZGPerlinGenerator alloc] init];
generator.octaves = 10;
generator.persistence = 0.5;
generator.zoom = 150;
// bitmap info
size_t width = size.width;
size_t height = size.height;
size_t bytesPerPixel = 4;
size_t bitsPerComponent = 8;
size_t bytesPerRow = width * bytesPerPixel;
CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big;
// allocate memory for the bitmap
UInt8* imgData = calloc(bytesPerRow, height);
// create RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create an RGBA bitmap context where the alpha component is ignored
CGContextRef ctx = CGBitmapContextCreate(imgData, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);
// iterate over pixels
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
// the current pixel index
size_t byte = x * bytesPerPixel + y * bytesPerRow;
// get noise data for given x & y
int value = round([generator perlinNoiseX:x y:y z:0 t:0]*255.0);
// limit values (not too sure of the range of values that the method outputs – this may not be needed)
if (value > 255) value = 255;
else if (value < 0) value = 0;
// write values to pixel components
imgData[byte] = value; // R
imgData[byte+1] = value; // G
imgData[byte+2] = value; // B
}
}
// get image
CGImageRef imgRef = CGBitmapContextCreateImage(ctx);
UIImage* img = [UIImage imageWithCGImage:imgRef];
// clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imgRef);
free(imgData);
return img;
}
UIGraphicsBeginImageContext(WithOptions) 自动使图像上下文成为当前上下文——因此您不需要使用它执行 UIGraphicsPushContext/UIGraphicsPopContext .
UIGraphicsBeginImageContext 使用 1.0 的比例 – 这意味着您使用的是像素大小,而不是点数。因此,您输出的图像可能不适合 2x 或 3x 显示器。您通常应该使用 UIGraphicsBeginImageContextWithOptions 代替,如果您只是操作,则使用 0.0(主屏幕比例)或 image.scale 的比例给定的图像(适用于您的 imageWithLumaMaskFromDark 方法)。
CGBitmapContextCreate 还将创建一个比例为 1.0 的上下文。如果您想将图像缩放到与屏幕相同的比例,您只需将输入的宽度和高度乘以屏幕比例即可:
CGFloat scale = [UIScreen mainScreen].scale;
size_t width = size.width*scale;
size_t height = size.height*scale;
然后在创建输出 UIImage 时提供比例:
UIImage* img = [UIImage imageWithCGImage:imgRef scale:scale orientation:UIImageOrientationUp];
如果您想在位图上下文中执行一些 CGContext 绘图调用,您还需要在绘图之前对其进行缩放,以便您可以在点坐标系中工作:
CGContextScaleCTM(ctx, scale, scale);
关于ios - 再次编辑时 Core Graphics 结果图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646076/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/