标签: cgcontext

将UIViews子视图展平为UIImage iPhone 3.0

我有一个UIView,它有几个UIImageViews作为子视图.这些子视图中的每一个都应用了不同的仿射变换.我想采取相当于我的UIView的屏幕截图,将其捕获为UIImage或其他图像表示.

我已经尝试过的方法,将图层渲染为CGContext:

[view.layer renderInContext:UIGraphicsGetCurrentContext()];
Run Code Online (Sandbox Code Playgroud)

不保留我的子视图的定位或其他仿射变换.

我真的很感激正确的方向踢.

iphone cocoa-touch cgcontext

6
推荐指数
1
解决办法
2695
查看次数

iPhone/iPad上的CGBitmapContextCreate

我有一个方法需要逐个像素地解析一堆大的PNG图像(PNG每个600x600像素).它似乎在模拟器上运行良好,但在设备(iPad)上,我在某些内部存储器复制功能中获得了EXC_BAD_ACCESS.看起来大小是罪魁祸首,因为如果我在较小的图像上尝试它,一切似乎都有效.这是以下方法的记忆相关肉.

+ (CGRect) getAlphaBoundsForUImage: (UIImage*) image 
{    
    CGImageRef imageRef = [image CGImage];

NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

unsigned char *rawData = malloc(height * width * 4);
memset(rawData,0,height * width * 4);

NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

/* non-memory related stuff */

free(rawData);
Run Code Online (Sandbox Code Playgroud)

当我在一堆图像上运行它时,它运行12次然后缩小,而在模拟器上运行没有问题.你们有什么想法吗?

memory iphone cgcontext ipad

6
推荐指数
1
解决办法
1万
查看次数

如何判断UIWebView何时完成绘制到上下文?

在我的代码中,我试图显示一个UIWebView正在加载的页面,然后,当它完成时,从Web视图捕获图像以便稍后缓存和显示(所以我不必重新加载和呈现网页) .

我有类似的东西:

CGContextRef context = CGBitmapContextCreate(…);
[[webView layer] renderInContext:context];

CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *image = [UIImage imageWithCGImage:imageRef];
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,由于UIWebView平铺,有时只有一半的页面在我捕获图像时被渲染到上下文中.

有没有办法检测或阻止UIWebView背景渲染线程,这样我才能在所有渲染完成后才能获取图像?


更新:可能是线程竞争条件是红色鲱鱼(从文档中无论如何都不清楚,无论UIWebView是自定义层还是CATiledLayer后台线程中的常规块).

这可能是一个无效问题(尽管对它和它的层setNeedsDisplay都有几种调用UIWebView).更改UIWebView渲染之前的界限似乎已经消除了"不绘制整个事物"的问题.

我仍然遇到了一个问题,即在旧规模上绘制了几块瓷砖,但是renderInContext:两次调用似乎已经足够了.

iphone core-graphics uiwebview cgcontext

6
推荐指数
1
解决办法
1974
查看次数

我如何从UIImage获得CGContextRef?

    CGContextRef context = ??; // get from UIImage *oldImage

    CGContextSaveGState(context);   
    CGRect drawRect = CGRectMake(0, 0, 320, 480);
    CGContextConcatCTM(context, transform);
    UIImage *newImage = [[UIImage alloc] init];
    CGContextDrawImage(context, drawRect, newImage.CGImage);
    [newImage drawInRect:CGRectMake(0, 0, 320, 480)];

    CGContextRestoreGState(context);
Run Code Online (Sandbox Code Playgroud)

简而言之,我想要做的是[UIImage - >进行一些转换 - >转换UIImage].

有任何想法吗?十分感谢!!

iphone uiimage cgcontext

6
推荐指数
1
解决办法
9378
查看次数

更改CGContext的背景颜色

我是处理CGContexts的新手,我只是想通过简单的触摸绘画和草图教程(http://blog.effectiveui.com/?p=8105)来解决问题.

当我改变CGContext的背景颜色时,我碰到了一堵砖墙.

我正在启动这样的conext:

 - (BOOL) initContext:(CGSize)size {
     int bitmapBytesPerRow;
     bitmapBytesPerRow = (size.width * 4);
     cacheContext = CGBitmapContextCreate (nil, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipLast);
     CGContextSetRGBFillColor(cacheContext, 1, 1, 1, 1);
     return YES;
 }
Run Code Online (Sandbox Code Playgroud)

并改变这样的笔画颜色和宽度:

UIColor *color = [UIColor whiteColor];
CGContextSetStrokeColorWithColor(cacheContext, [color CGColor]);
CGContextSetLineCap(cacheContext, kCGLineCapRound); 
CGContextSetLineWidth(cacheContext, 4);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用黑色(在初始或在绘图/笔划设置部分中)更改背景颜色时,CGContextSetRGBFillColor(cacheContext, 1, 1, 1, 1);没有任何效果.

是否有人能够指出我正确的方向或更好/正确的地方拨打电话,或正确的使用电话?非常感谢您的时间!

iphone background paint objective-c cgcontext

6
推荐指数
1
解决办法
8620
查看次数

跟踪多点触控中的触控点

我正在制作一个绘图项目,我想支持多点触控,我已经通过在线文档,建议跟踪触摸点,我做了,但我没有得到理想的行为.我没有得到直线.

以下是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    red = 0.0/255.0;
    green = 0.0/255.0;
    blue = 0.0/255.0;
    brush = 10.0;
    opacity = 1.0;

     self.view.multipleTouchEnabled = YES;

     touchPaths = [NSMutableDictionary dictionary];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    for (UITouch *touch in touches)
    {
        NSString *key = [NSString stringWithFormat:@"%d", (int) touch];
        lastPoint = [touch locationInView:self.view];

        [touchPaths setObject:[NSValue valueWithCGPoint:lastPoint] forKey:key];
    }

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    for (UITouch …
Run Code Online (Sandbox Code Playgroud)

core-graphics multi-touch uikit cgcontext ios

6
推荐指数
1
解决办法
1493
查看次数

SDK 10.10中的graphicsPort的Quartz 2D替代

场景:

我正在编辑一些CGImages并且很奇怪,直到现在我才开始在绘制它们之前NSImage从那些CGImages中创建s .所以我试着改变代码,以便我CGImage直接将s 绘制成NSGraphicsContext.currentContext().graphicsPort.如你所见,我在这里使用Swing但是我认为Objective-C也存在同样的问题.

看一下我看到的文档graphicsPort将在10.10中被弃用,但我找不到另一种方法来获取当前的CGContext,NSGraphicsContext也没有办法创建一个CGContext来自NSGraphicsContext.

问题:

有谁知道检索上下文的正确方法是什么?如果不是 - 有没有办法将graphicsPort's COpaquePointer,显然是一个初始化的NSPipe对象)转换为CGContextRef?虽然使用已被标记为已弃用的代码会非常不令人满意,但我会考虑它,直到有更好的解决方案.

deprecated cgcontext quartz-2d

6
推荐指数
1
解决办法
582
查看次数

核心图形坐标系原点混乱

我对 UIKit 和 Core Graphics 之间的坐标系差异有点困惑。Apple 文档指出 UIKit 使用 ULO(左上原点),而 Core Graphics 使用 LLO(左下原点)。

然而,正如一篇关于坐标系的文章中所指出的,值得注意的是 UIKit

在调用 -drawRect 之前有用地翻转坐标系:

当我在 (0,0) 处绘制一个矩形时,它确实位于左上角,所以事情看起来确实翻转了。但是,当我使用 CGContextDrawImage() 绘制图像时,它仍然是翻转的。我知道我可以使用 drawAtPoint() 来避免颠倒的图像,但我只是对坐标系感到好奇:如果它是由 UIKit 翻转的,那么如果将其绘制到翻转的上下文中,为什么图像仍然是颠倒的?另外,有没有办法获得未翻转的坐标系?也就是说,有没有一种方法可以不通过UIKit来创建CGContext?

谢谢!

core-graphics cgcontext coordinate-systems

6
推荐指数
1
解决办法
986
查看次数

用UIGraphicsImageRenderer的jpegData替换UIImageJPEGRepresentation

我正在努力当用户需要从相机中的照片添加宽彩色照片在iOS的10支持,我需要使用支持新的色彩空间来保存照片数据的新API - UIGraphicsImageRendererjpegData替代UIImageJPEGRepresentation.

我遇到了一些图像方向的麻烦.在我的iPad上纵向拍照,图像绘制不正确.请参阅以下评论:

旧API:

let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let imageData = UIImageJPEGRepresentation(image, 1)
Run Code Online (Sandbox Code Playgroud)

新API:

let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let cgImage = image.cgImage!
let ciImage = CIImage(cgImage: cgImage)

let format = UIGraphicsImageRendererFormat()
format.scale = 1
format.prefersExtendedRange = true

let renderer = UIGraphicsImageRenderer(bounds: ciImage.extent, format: format)
let imageData = renderer.jpegData(withCompressionQuality: 1, actions: { context in
    context.cgContext.draw(cgImage, in: ciImage.extent) //draws flipped horizontally
    //image.draw(at: .zero) //draws rotated 90 degrees leaving black at bottom
    //image.draw(in: …
Run Code Online (Sandbox Code Playgroud)

uikit cgcontext ios cgcontextdrawimage

6
推荐指数
1
解决办法
1031
查看次数

使用 Swift 在位图图形上下文(在 macOS 上)中添加文本

我的项目是

\n\n
    \n
  1. 将一些图像裁剪粘贴到位图图形上下文上
  2. \n
  3. 添加一些文本
  4. \n
  5. 将位图另存为 JPEG 图像
  6. \n
\n\n

我对 1) 和 3) 没有问题,但我可以 \xe2\x80\x99t 找到一种将文本添加到位图图形上下文的方法。我花了很多时间阅读一些帖子,但没有成功。\n文档(https://developer.apple.com/reference/coregraphics/cgcontext/1586505-showtextatpoint)说我应该考虑核心文本,但我\n不知道如何应该使用 Core Text 将一些文本添加到我的\n位图上下文中。我还读到 NSString 的绘制实例方法允许\n在当前图形上下文中写入文本,但据我所知当前\n图形上下文与我的位图图形上下文无关?

\n\n

需要明确的是,我对在屏幕上绘制文本不感兴趣,我感兴趣的是在位图中绘制文本并将该位图保存在文件中。

\n\n
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()\n// Create the bitmap graphic context\nvar gc = CGContext(data: nil, width: 400, height: 400, bitsPerComponent: 8, bytesPerRow: 0,\n                   space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)\n\n// draw an image (croppedCGImage is a CGImage instance)\nvar rect = CGRect(x: 0, y:0, width:200, height: 200)\ngc?.draw(croppedCGImage, in: rect)\n\n// draw some text ????\n// how to do that …
Run Code Online (Sandbox Code Playgroud)

macos core-graphics bitmap cgcontext swift

6
推荐指数
0
解决办法
769
查看次数