我有一个 Objective C 项目,它在某一时刻从您的照片库或相机中拍摄一张照片,然后将它们切成小方块(瓷砖),并最终从切碎的方块中创建新的 UIImage 对象。
该代码在我的旧 iPhone 5 和 iPad Mini 2 上完美运行。
然而,在我的 iPhone 7 Plus 上,它会产生空白输出(而不是我输入的部分内容)。iPad Mini 2 和 iPhone 7 Plus 都运行 iOS 10.3.3,并且都是 64 位设备。
完整的功能在这里:
-(void) createTilesFromImage:(UIImage*)img inRect:(CGRect)rect inContext:(CGContextRef)context {
if (tilesDefined) {
return;
}
tilesDefined = YES;
// Tileboard layer
CGSize tileboardsize = rect.size;
tileboardrect = rect;
// Chop image
CGLayerRef imagetochop = CGLayerCreateWithContext(context, tileboardsize, NULL);
CGContextRef ctx2 = CGLayerGetContext(imagetochop);
UIGraphicsPushContext(ctx2);
[img drawInRect:CGRectMake(0, 0, tileboardsize.width, tileboardsize.height)];
UIGraphicsPopContext();
// Tile layer size (CG & CA)
tilesize = CGSizeMake(ceilf(tileboardsize.width / dims.x),ceilf( tileboardsize.height / dims.y ));
// Bitmap context
CGContextRef bmpContext = [[QPDrawing shared] newCGBitmapContextWithSize:tilesize withData:YES];
// Create tile layers
tiles = [[NSMutableArray alloc] initWithCapacity:dims.x * dims.y];
int i = 0;
for (int y = 0; y < dims.y; y++) {
for (int x = 0; x < dims.x; x++) {
tileLayers[i] = CGLayerCreateWithContext(context, tilesize, NULL);
CGContextRef squarecontext = CGLayerGetContext(tileLayers[i]);
CGPoint offset = CGPointMake(x * tileboardsize.width / dims.x * -1, y * tileboardsize.height / dims.y * -1);
// Invert the layer prior to drawing
CGContextTranslateCTM(squarecontext, 0, tilesize.height);
CGContextScaleCTM(squarecontext, 1.0, -1.0);
CGContextDrawLayerAtPoint(squarecontext, offset, imagetochop);
CGContextDrawLayerInRect(bmpContext, (CGRect){0,0,tilesize.width,tilesize.height}, tileLayers[i]);
CGImageRef imageRef = CGBitmapContextCreateImage( bmpContext );
[tiles addObject: [UIImage imageWithCGImage: imageRef]];
CGImageRelease(imageRef);
i++;
}
}
// Cleanup
CGLayerRelease(imagetochop);
CGContextRelease(bmpContext);
rect = CGRectInset(rect, 2, 2);
CGContextAddRect(context, rect);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:1 green:0 blue:0 alpha:1].CGColor);
CGContextSetLineWidth(context, 2);
CGContextStrokePath(context);
}
这在创建 CGContextRef 时调用了一个外部部分,bmpContext -- newCGBitmapContextWithSize :tileSize:withData。该位的代码如下:
- (CGContextRef) newCGBitmapContextWithSize:(CGSize)size withData:(BOOL)includeData {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int width = (int)ceilf(size.width);
int height = (int)ceilf(size.height);
size_t bitsPerComponent = 8;
size_t bytesPerPixel = 4;
size_t bytesPerRow = (width * bitsPerComponent * bytesPerPixel + 7) / 8;
size_t dataSize = bytesPerRow * height;
unsigned char *data = NULL;
if (includeData) {
data = malloc(dataSize);
memset(data, 0, dataSize);
}
CGContextRef context = CGBitmapContextCreate(data, size.width, size.height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
return context;
}
起初我认为是“ drawInRect ”调用不知何故不起作用,现在我确信情况并非如此。我认为它与CGBitmapContextCreate调用有关。
谢谢!