将jpg UIImage转换为位图UIImage? [英] convert jpg UIImage to bitmap UIImage?

查看:94
本文介绍了将jpg UIImage转换为位图UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让用户平移/缩放静态图像,并在主图像上带有一个选择矩形,并为放大"图像使用单独的UIView.

I'm trying to let a user pan/zoom through a static image with a selection rectangle on the main image, and a separate UIView for the "magnified" image.

放大的" UIView实现了drawRect:

The "magnified" UIView implements drawRect:

// rotate selectionRect if image isn't portrait internally
CGRect tmpRect = selectionRect;
if ((image.imageOrientation == UIImageOrientationLeft || image.imageOrientation == UIImageOrientationLeftMirrored || image.imageOrientation == UIImageOrientationRight || image.imageOrientation == UIImageOrientationRightMirrored)) {
    tmpRect = CGRectMake(selectionRect.origin.y,image.size.width - selectionRect.origin.x - selectionRect.size.width,selectionRect.size.height,selectionRect.size.width);
} 

// crop and draw
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], tmpRect);
[[UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation] drawInRect:rect];
CGImageRelease(imageRef);

在这方面的表现是残酷的.它将其92%的时间都花在了[UIImage drawInRect]中.

The performance on this is atrocious. It spends 92% of its time in [UIImage drawInRect].

更深一层,在ripc_AcquireImage中为84.5%,在ripc_RenderImage中为7.5%.

Deeper, that's 84.5% in ripc_AcquireImage and 7.5% in ripc_RenderImage.

ripc_AcquireImage是对jpg的51%解码,对30%的向上采样.

ripc_AcquireImage is 51% decoding the jpg, 30% upsampling.

所以...我想我的问题是避免这种情况的最佳方法是什么?一种选择是不接受jpg开始,这是对某些事情的真正解决方案[ala captureStillImageAsynchronouslyFromConnection没有JPG中介].但是,如果我要从相机胶卷上取下UIImage,请说...是否有一种干净的方法来转换UIImage-jpg?正在将其转换为正确的东西(本质上是否有"cacheAsBitmap"标志可以做到这一点?)

So...I guess my question is what's the best way to avoid this? One option is to not take in a jpg to start, and that is a real solution for some things [ala captureStillImageAsynchronouslyFromConnection without JPG intermediary ]. But if I'm getting a UIImage off the camera roll, say...is there a clean way to convert the UIImage-jpg? Is converting it even the right thing (is there a "cacheAsBitmap" flag somewhere that would do that, essentially?)

推荐答案

显然,这不仅是JPG问题,我认为这还没有理想的本机表示形式"支持.

Apparently this isn't just a JPG issue—it's anything that's not backed by "ideal native representation", I think.

我在以下方面取得了很好的成功(显着的性能改进):

I'm having good success (significant performance improvements) with just the following:

UIGraphicsBeginImageContext(image.size);
[image drawAtPoint:CGPointZero];
image = [UIGraphicsGetImageFromCurrentImageContext() retain];
UIGraphicsEndImageContext();

对于从相机胶卷和从相机胶卷拍摄的东西.

For both things taken from the camera roll and from the camera.

这篇关于将jpg UIImage转换为位图UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆