首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS -可以缓存CGContextDrawImage吗?

iOS -可以缓存CGContextDrawImage吗?
EN

Stack Overflow用户
提问于 2012-03-31 11:13:36
回答 1查看 1.3K关注 0票数 1

我使用了时间配置文件工具来确定95%的时间用于调用函数CGContextDrawImage。

在我的应用程序中,有很多重复的图像被重复地从精灵地图上剪下来并绘制到屏幕上。我想知道是否有可能将CGContextDrawImage的输出缓存到NSMutableDictionay中,然后如果再次请求相同的sprite,就可以将其从缓存中提取出来,而不是再次执行裁剪和呈现它的所有工作。这就是我所拥有的,但我并没有成功:

定义

代码语言:javascript
复制
if(cache == NULL) cache = [[NSMutableDictionary alloc]init];
//Identifier based on the name of the sprite and location within the sprite.
NSString* identifier = [NSString stringWithFormat:@"%@-%d",filename,frame];

添加到缓存

代码语言:javascript
复制
 CGRect clippedRect = CGRectMake(0, 0, clipRect.size.width, clipRect.size.height);
    CGContextClipToRect( context, clippedRect);

    //create a rect equivalent to the full size of the image
    //offset the rect by the X and Y we want to start the crop
    //from in order to cut off anything before them
    CGRect drawRect = CGRectMake(clipRect.origin.x * -1,
                                 clipRect.origin.y * -1,
                                 atlas.size.width,
                                 atlas.size.height);

    //draw the image to our clipped context using our offset rect
    CGContextDrawImage(context, drawRect, atlas.CGImage);
    [cache setValue:UIGraphicsGetImageFromCurrentImageContext() forKey:identifier];
    UIGraphicsEndImageContext();

呈现缓存的雪碧可能有一种更好的方法来呈现CGImage,这是我的最终缓存目标,但目前我只是想成功地呈现缓存的映像,但是这并不成功。

代码语言:javascript
复制
 UIImage* cachedImage = [cache objectForKey:identifier];

if(cachedImage){
    NSLog(@"Cached %@",identifier);

    CGRect imageRect =  CGRectMake(0,
                                   0,
                                   cachedImage.size.width,
                                   cachedImage.size.height);

    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, 0);
    else
        UIGraphicsBeginImageContext(imageRect.size);

    //Use draw for now just to see if the image renders out ok
    CGContextDrawImage(context, imageRect, cachedImage.CGImage);
     UIGraphicsEndImageContext();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-31 11:22:51

是的,可以缓存呈现的图像。下面是它是如何完成的示例:

代码语言:javascript
复制
+ (UIImage *)getRenderedImage:(UIImage *)image targetSize:(CGSize)targetSize
{
    CGRect targetRect = CGRectIntegral(CGRectMake(0, 0, targetSize.width, targetSize.height)); // should be used by your drawing code
    CGImageRef imageRef = image.CGImage; // should be used by your drawing code
    UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    // TODO: draw and clip your image here onto context
    // CGContextDrawImage CGContextClipToRect calls
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();
    return newImage;
}

这样,就可以得到资源映像的呈现副本。因为在呈现过程中,你有上下文,你可以自由地做任何你想做的事情。您只需要事先确定输出大小。

生成的映像只是UIImage的一个实例,您可以将其放入NSMutableDictionary中供以后使用。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9955378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档