- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我希望以一个 Core Graphics CGImageRef 结束,它以覆盖的 -(void)drawRect:(CGRect)rect 方法绘制到屏幕上。这是我到目前为止所得到的。
- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
const CGFloat _x = CGRectGetMinX(rect);
const CGFloat _y = CGRectGetMinY(rect);
const CGFloat _w = CGRectGetWidth(rect);
const CGFloat _h = CGRectGetHeight(rect);
const CGFloat scale = [[UIScreen mainScreen] scale];
const int pixelsWide = (_w * scale);
const int pixelsHigh = (_h * scale);
const size_t colorValues = 4;
const int rawMemorySize = (pixelsWide * pixelsHigh * colorValues);
// raw pixel data memory
UInt8 pixelData[rawMemorySize];
// fill the raw pixel buffer with arbitrary gray color for test
for(size_t ui = 0; ui < rawMemorySize; ui++)
{
pixelData[ui] = 255; // black
}
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CFDataRef rgbData = CFDataCreate(NULL, pixelData, rawMemorySize);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);
CGImageRef rgbImageRef = CGImageCreate(pixelsWide, pixelsHigh, 8, (colorValues * colorValues), (pixelsWide * colorValues), colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault);
CFRelease(rgbData);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorspace);
//Draw the bitmapped image
CGContextDrawImage(currentContext, CGRectMake(_x, _y, pixelsWide, 64), rgbImageRef);
CGImageRelease(rgbImageRef);
}
我在 for 循环中抛出一个 EXC_BAD_ACCESS 错误。
我想我很接近,但我不太确定。任何想法可能是罪魁祸首?提前谢谢你:)
最佳答案
还有一些事情需要注意。
drawRect 的rect
参数只是脏矩形,它可能小于self.bounds
。使用 self.bounds
除非你的代码被优化为只绘制脏矩形。
在堆栈上分配 pixelData
数组是个坏主意,并且可能是错误的原因。这完全取决于 View 有多大。我怀疑 AaronGolden 使用的 View 比您使用的 View 小,因此没有任何问题。 calloc
数组更安全,并在结束使用后释放它。请注意,calloc
会将数组清除为透明黑色。
这是我过去用来做这类事情的一些示例代码。即使像素数组是使用 calloc
创建的,您仍然可以像访问标准数组一样访问它(例如 pixels[100] = 0xff0000ff;
是完全有效的)。但是,出于性能原因,我通常使用指针来访问单个像素。
const CGFloat scale = [[UIScreen mainScreen] scale];
int width = self.bounds.size.width * scale;
int height = self.bounds.size.height * scale;
uint32_t *pixels = calloc( width * height, sizeof(uint32_t) );
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate( pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast );
uint8_t *bufptr = (uint8_t *)pixels;
for ( int y = 0; y < height; y++)
{
for ( int x = 0; x < width; x++ )
{
bufptr[0] = redValue;
bufptr[1] = greenValue;
bufptr[2] = blueValue;
bufptr[3] = alphaValue;
bufptr += 4;
}
}
CGImageRef newCGImage = CGBitmapContextCreateImage( context );
CGContextRelease( context );
CGColorSpaceRelease( colorSpace );
free( pixels );
关于c++ - 如何从自定义数据流编写 CoreGraphics CGImageRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792899/
Xcode 10 更改了搜索头文件的方法。具体来说,它给出了警告: Traditional headermap style is no longer supported; please migrate
我的基于 Objective-C 的项目中有一些 swift 代码文件。 我可以编译它,它们相互配合得很好。 swift 代码可以 导入 CoreGraphics.CGBase 然而,当我在调试swi
我的形状周围有这条线: 问题是,它的厚度显然从 1px 变成了 2px。这是我正在使用的代码: CGContextRef context = UIGraphicsGetCurrentContext()
我有以下代码,似乎唯一使用整行的最后一种颜色......我希望颜色在整个过程中不断变化。有什么想法吗? CGContextSetLineWidth(ctx, 1.0);
我试图在 CG 中获得透明背景,但它总是显示为黑色。 我有以下代码: - (id)initWithFrame:(CGRect)frame { if ((self = [super initWi
我使用以下代码对加载的图像执行一些操作,但我发现在视网膜显示屏上时显示变得模糊 - (UIImage*)createImageSection:(UIImage*)image section:(CGRe
我正在尝试绘制具有2px透明边框的图像: CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(
我已经使用CoreGraphics绘制了两个四边形(4面)形状的路径。总共有6个点,path1使用前4个点,path2使用后4个点,因此都共享2个点。 代码如下 - (void)drawRect:(C
我正在编写一个左侧有图例的 ViewController(标签和彩色框的垂直列表:Category1:黄色,Category2:gree,Category 3:蓝色,等等......) 用户可以点击列
我的形状周围有这条线: 问题是,它的厚度显然从 1px 变成了 2px。这是我正在使用的代码: CGContextRef context = UIGraphicsGetCurrentContext()
我有一个名为 myView 的 UIView @interface MyView : UIView { UIImage *myPic; NSMutableArray *myDra
我需要帮助来绘制这样的东西: 有人告诉我,灰色背景条和紫色条应该绘制在不同的图层上。然后,表示一本书章节(此 slider 所涉及的内容)的点将位于这两层之上的一层上。 我已经完成了在事件栏上创建渐变
有人可以帮我用 CoreGraphics 绘制一个特定的渐变吗?只要给出线索就足够了。示例代码值得我致以最深的敬意。我需要这个形状: 我尝试了以下代码: -(UIImage *)imageWithGr
我的 CGContext 有一个路径和一个线宽,这在我的 UIView 中绘制了一个漂亮的黑色路径轮廓。 .伟大的。现在我试着给这个笔划上色,虽然 CGContextSetStrokeColorWit
我有一个UIBezierPath,它是一个弧线。我只想在图形上下文中绘制笔划(即圆弧的笔划,不包括连接圆弧终点和起点的线) 完成此操作后,我可以向上下文添加线性渐变,从而有效地在圆弧笔划上绘制渐变。
我正在尝试使用核心图形创建两个矩形路径。当我尝试使用颜色填充路径时,重叠的路径不会填充颜色。 使用的代码是 - (void)drawRect:(CGRect)rect { CGPoint to
编辑:我认为最好的是将代码转换为 CoreAnimation。该代码将如何转换为该方法?我认为问题在于设备 FPS 存在差异。 我一直在为 iOS 搜索自定义进度圈并遇到这个库:https://git
我肯定遗漏了 Core Graphics 上下文的某些部分,无法自行清除。这是我在 Xcode Playground 中的代码 import UIKit class MyView08 : UIView
我不确定是否可以提高图像质量,但以下代码在 PDF 中显示的图像质量非常差。我知道它用于从 View 生成图像的标准代码,但我可以做些什么来指定图像质量或改进它吗? - (void)renderVie
此代码来自 Apple 的 WWDC 2011 session 318 - 深入了解 iOS 性能并使用 CoreGraphics 从服务器托管图像创建缩略图。 CGImageSourceRef sr
我是一名优秀的程序员,十分优秀!