如何在 iOS 中将多个 UIImage 添加到一个基本 UIImage
How to add several UIImages to one base UIImage in iOS
我知道必须有一种方法可以拍摄一个基本图像(在我的例子中是一个透明的 4096x4096 基本层)并在不同位置向它添加几十个(最多 100 个)非常小的 (100x100) 图像。但是我不知道该怎么做:假设 imageArray
有很多元素,每个元素都包含一个具有以下结构的 NSDictionary:
@{
@"imagename":imagename,
@"x":xYDict[@"x"],
@"y":xYDict[@"y"]
}
现在,这是代码(它不起作用。我只有一个透明屏幕......性能仍然很糟糕......循环中每个 "image" 大约 1-2 秒...和大量内存使用。
NSString *PNGHeatMapPath = [[myGizmoClass dataDir] stringByAppendingPathComponent:@"HeatMap.png"];
@autoreleasepool
{
CGSize theFinalImageSize = CGSizeMake(4096, 4096);
NSLog(@"populateHeatMapJSON: creating blank image");
UIGraphicsBeginImageContextWithOptions(theFinalImageSize, NO, 0.0);
UIImage *theFinalImage = UIGraphicsGetImageFromCurrentImageContext();
for (NSDictionary *theDict in imageArray)
{
NSLog(@"populateHeatMapJSON: adding: %@",theDict[@"imagename"]);
UIImage *image = [UIImage imageNamed:theDict[@"imagename"]];
[image drawInRect:CGRectMake([theDict[@"x"] doubleValue],[theDict[@"y"] doubleValue],image.size.width,image.size.height)];
}
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(theFinalImage);
theFinalImage = nil;
[imageData writeToFile:PNGHeatMapPath atomically:YES];
NSLog(@"populateHeatMapJSON: PNGHeatMapPath = %@",PNGHeatMapPath);
}
我知道我误解了 UIGraphics
上下文...有人可以帮助我吗?我试图在某些 x,y 位置的空白透明 canvas 上叠加一堆 UIImage,获取最终图像并保存。
PS:这也是..如果模拟器有任何问题的话......漏水了。它上升到 2 或 3 演出...
提前致谢。
编辑:
我确实找到了这个并尝试了它,但它需要我重写整个图像,将该图像复制到一个新图像并添加到那个新图像。希望获得 "base image"、"add this image"、"add that image"、"add the other image" 并获得新的基本图像而无需所有复制:
这是最好的了吗?
+(UIImage*) drawImage:(UIImage*) fgImage
inImage:(UIImage*) bgImage
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0);
[bgImage drawInRect:CGRectMake( 0, 0, bgImage.size.width, bgImage.size.height)];
[fgImage drawInRect:CGRectMake( point.x, point.y, fgImage.size.width, fgImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
您在实际绘制任何东西之前就得到了图像 UIGraphicsGetImageFromCurrentImageContext()
。将此调用移至循环之后和 UIGraphicsEndImageContext()
之前
你的画实际发生在直线上
[image drawInRect:CGRectMake([theDict[@"x"] doubleValue],[theDict[@"y"] doubleValue],image.size.width,image.size.height)];
函数 UIGraphicsGetImageFromCurrentImageContext()
为您提供上下文当前内容的图像,因此您需要确保在抓取图像之前已完成所有绘图。
我知道必须有一种方法可以拍摄一个基本图像(在我的例子中是一个透明的 4096x4096 基本层)并在不同位置向它添加几十个(最多 100 个)非常小的 (100x100) 图像。但是我不知道该怎么做:假设 imageArray
有很多元素,每个元素都包含一个具有以下结构的 NSDictionary:
@{
@"imagename":imagename,
@"x":xYDict[@"x"],
@"y":xYDict[@"y"]
}
现在,这是代码(它不起作用。我只有一个透明屏幕......性能仍然很糟糕......循环中每个 "image" 大约 1-2 秒...和大量内存使用。
NSString *PNGHeatMapPath = [[myGizmoClass dataDir] stringByAppendingPathComponent:@"HeatMap.png"];
@autoreleasepool
{
CGSize theFinalImageSize = CGSizeMake(4096, 4096);
NSLog(@"populateHeatMapJSON: creating blank image");
UIGraphicsBeginImageContextWithOptions(theFinalImageSize, NO, 0.0);
UIImage *theFinalImage = UIGraphicsGetImageFromCurrentImageContext();
for (NSDictionary *theDict in imageArray)
{
NSLog(@"populateHeatMapJSON: adding: %@",theDict[@"imagename"]);
UIImage *image = [UIImage imageNamed:theDict[@"imagename"]];
[image drawInRect:CGRectMake([theDict[@"x"] doubleValue],[theDict[@"y"] doubleValue],image.size.width,image.size.height)];
}
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(theFinalImage);
theFinalImage = nil;
[imageData writeToFile:PNGHeatMapPath atomically:YES];
NSLog(@"populateHeatMapJSON: PNGHeatMapPath = %@",PNGHeatMapPath);
}
我知道我误解了 UIGraphics
上下文...有人可以帮助我吗?我试图在某些 x,y 位置的空白透明 canvas 上叠加一堆 UIImage,获取最终图像并保存。
PS:这也是..如果模拟器有任何问题的话......漏水了。它上升到 2 或 3 演出... 提前致谢。
编辑: 我确实找到了这个并尝试了它,但它需要我重写整个图像,将该图像复制到一个新图像并添加到那个新图像。希望获得 "base image"、"add this image"、"add that image"、"add the other image" 并获得新的基本图像而无需所有复制:
这是最好的了吗?
+(UIImage*) drawImage:(UIImage*) fgImage
inImage:(UIImage*) bgImage
atPoint:(CGPoint) point
{
UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0);
[bgImage drawInRect:CGRectMake( 0, 0, bgImage.size.width, bgImage.size.height)];
[fgImage drawInRect:CGRectMake( point.x, point.y, fgImage.size.width, fgImage.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
您在实际绘制任何东西之前就得到了图像 UIGraphicsGetImageFromCurrentImageContext()
。将此调用移至循环之后和 UIGraphicsEndImageContext()
你的画实际发生在直线上
[image drawInRect:CGRectMake([theDict[@"x"] doubleValue],[theDict[@"y"] doubleValue],image.size.width,image.size.height)];
函数 UIGraphicsGetImageFromCurrentImageContext()
为您提供上下文当前内容的图像,因此您需要确保在抓取图像之前已完成所有绘图。