그리기 코드의 속도를 높이려고합니다. 그래픽 컨텍스트를 생성하고, 현재 이미지를 드로잉하고, 드로잉하는 대신, 일부 픽셀 데이터를 사용하여 컨텍스트를 생성하고이를 직접 수정하려고합니다. 문제는 핵심 그래픽에 익숙하지 않고 초기 이미지를 만들 수 없다는 것입니다. 나는 단지 튼튼한 빨간 이미지를 원하지만 나는 아무것도 얻지 못합니다. 다음은 초기 이미지에 대해 사용하는 것입니다. 문제는 나머지 코드와 동일하다고 가정합니다.Core Graphics 컨텍스트에서 재사용 가능한 이미지를 생성하면 아무 것도 표시되지 않습니다.
pixels = malloc(320*460*4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, 320, 460, 8, 4*320, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextAddRect(context, CGRectMake(0, 0, 320, 460));
CGContextFillPath(context);
trace = [[UIImageView alloc] initWithImage:UIGraphicsGetImageFromCurrentImageContext()];
CGContextRelease(context);
편집 : UIGraphicsGetImageFromCurrentImageContext()
문제로 밝혀졌다. 작업 솔루션은 다음, 그러나 이것은 더 빠른 우선 들어 UIGraphicsBeginImageContext()
pixels = malloc(320*460*4);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, 320, 460, 8, 4*320, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextAddRect(context, CGRectMake(0, 0, 320, 460));
CGContextFillPath(context);
CGDataProviderRef dp = CGDataProviderCreateWithData(NULL, pixels, 320*460*4, NULL);
CGImageRef img = CGImageCreate(320, 460, 8, 32, 4*320, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, dp, NULL, NO, kCGRenderingIntentDefault);
trace = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:img]];
CGImageRelease(img);
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
0 원점 및 320 × 460 크기를 하드 코딩하는 대신 런타임에 응용 프로그램의 창 공간 (즉, 화면에서 상태 표시 줄을 뺀 사각형)을 가져와야합니다. 우선 코드가 가로 (예 : 480 × 300) 화면을 처리하지 않습니다. 또 다른 이유로는 앱이 자동으로 더 큰 화면 크기 (예 : 태블릿)를 처리하는 것이 좋습니다. 아마'- [UIScreen applicationFrame :]'을 사용할 필요가 있습니다 : http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html#//apple_ref/occ/ instp/UIScreen/applicationFrame –