为什么 IKImageBrowserView 调整图像大小的速度比我快得多?

发布于 2024-10-15 04:23:27 字数 1900 浏览 2 评论 0原文

这是我的图像调整大小代码:

CALayer *newCALayer = [[CALayer layer] retain];
NSImage* image = [[NSImage alloc] initWithData:[NSData  dataWithContentsOfFile:path]];
CGImageRef newCGImageFullResolution = [image CGImageForProposedRect:nil context:nil hints:nil];
CGContextRef context = CGBitmapContextCreate(NULL, drawRect.size.width, drawRect.size.height,
                                             CGImageGetBitsPerComponent(newCGImageFullResolution),
                                             CGImageGetBytesPerRow(newCGImageFullResolution),
                                             CGImageGetColorSpace(newCGImageFullResolution),
                                             CGImageGetAlphaInfo(newCGImageFullResolution));
CGContextDrawImage(context, CGRectMake(0, 0, drawRect.size.width, drawRect.size.height), newCGImageFullResolution);
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
newCALayer.contents = (id)scaledImage;
CGImageRelease(scaledImage);
newCALayer.contentsGravity = kCAGravityResizeAspect;
newCALayer.opacity = 0.0;
newCALayer.anchorPoint = CGPointMake(0.0f,0.0f);
newCALayer.frame = CGRectMake( 0.0, 
          0.0, 
          [Singleton sharedSingleton].fullscreenRect.size.width,
          [Singleton sharedSingleton].fullscreenRect.size.height);
[newCALayer setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];
//CGImageRelease(cgImageFullResolution); (bonus points if you can explain why I can't release this! I mean, I can release the scaled image ok??)
CGContextRelease(context);
[image release];

我从后台线程执行所有这些操作,以便预加载图片,以便我的 GUI 感觉敏捷。需要做一些工作来实现同步以及未设置的内容,以便 CALayers 最终出现在视图中。

但我相信描述它有多快的术语应该是“它是一只狗”。

与 IKImageView 相比,它弹出图像缩略图的速度比我滚动的速度还要快。

有人对如何比我现在做的更好地处理这个问题有一些建议吗?

换句话说,我的问题是我想要一个超快的用户体验。我相信实现这一点的方法是通过将东西预加载到 CALayers (这可能是错误的?我尝试了 NSImageView 和一些 IK 的东西,但至少 CALayer 比那更好)。

This is my image resize code:

CALayer *newCALayer = [[CALayer layer] retain];
NSImage* image = [[NSImage alloc] initWithData:[NSData  dataWithContentsOfFile:path]];
CGImageRef newCGImageFullResolution = [image CGImageForProposedRect:nil context:nil hints:nil];
CGContextRef context = CGBitmapContextCreate(NULL, drawRect.size.width, drawRect.size.height,
                                             CGImageGetBitsPerComponent(newCGImageFullResolution),
                                             CGImageGetBytesPerRow(newCGImageFullResolution),
                                             CGImageGetColorSpace(newCGImageFullResolution),
                                             CGImageGetAlphaInfo(newCGImageFullResolution));
CGContextDrawImage(context, CGRectMake(0, 0, drawRect.size.width, drawRect.size.height), newCGImageFullResolution);
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
newCALayer.contents = (id)scaledImage;
CGImageRelease(scaledImage);
newCALayer.contentsGravity = kCAGravityResizeAspect;
newCALayer.opacity = 0.0;
newCALayer.anchorPoint = CGPointMake(0.0f,0.0f);
newCALayer.frame = CGRectMake( 0.0, 
          0.0, 
          [Singleton sharedSingleton].fullscreenRect.size.width,
          [Singleton sharedSingleton].fullscreenRect.size.height);
[newCALayer setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];
//CGImageRelease(cgImageFullResolution); (bonus points if you can explain why I can't release this! I mean, I can release the scaled image ok??)
CGContextRelease(context);
[image release];

I am doing all of this from a background thread in order to preload pictures so my GUI feels snappy. It took some work getting synchronization and what not set up so the CALayers ends up in view.

But I believe the term for describing how fast this is would be "it's a dog".

Comparing to IKImageView - that thing flings up thumbnails of images faster than I can scroll.

Does anybody have some suggestions for how to handle this better than I am doing it now?

In other words, my problem is that I want to have a super-fast UX. I believe the way to accomplish this is by preloading things to CALayers (this may be wrong? I tried NSImageView and some IK-stuff, but at least CALayer is better than that).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-10-22 04:23:27

ImageKit 可能使用 CGImageSourceCreateThumbnailAtIndex() 来快速获取适合目标的图像,而不是读取整个图像文件。

ImageKit is probably using CGImageSourceCreateThumbnailAtIndex() to quickly get an image appropriate to the destination, rather than reading in the entire image file.

孤者何惧 2024-10-22 04:23:27

这里:

NSImage *image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
[image setScalesWhenResized:YES]; // *
[image setDataRetained:YES]; // *
[image setSize:desiredNewSize];

然后按原样使用图像。

至于为什么你的应用程序很慢,请在 Instruments 下运行它。这将具体告诉您您所使用的大部分处理器时间都花在哪里 - 毕竟它可能不在您的扩展代码中。

*自 10.6 起,这些消息没有任何用处并且已被弃用,因此如果您需要 Snow Leopard 或更高版本,可以忽略它们。

Here:

NSImage *image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
[image setScalesWhenResized:YES]; // *
[image setDataRetained:YES]; // *
[image setSize:desiredNewSize];

Then use the image as it is.

As for why your app is slow, run it under Instruments. That will tell you specifically where you are spending the majority of the processor time you use—it may not be in your scaling code after all.

*Since 10.6, these messages do nothing useful and are deprecated, so you can omit them if you are requiring Snow Leopard or later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文