使用CGImageSourceCreateThumbnailAtIndex从UIImage创建缩略图 [英] Creating a thumbnail from UIImage using CGImageSourceCreateThumbnailAtIndex

查看:135
本文介绍了使用CGImageSourceCreateThumbnailAtIndex从UIImage创建缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用功能 CGImageSourceCreateThumbnailAtIndex UIImage 创建缩略图.我所拥有的只是 UIImage 本身.该图像是 UIView 上的快照.

I want to use the function CGImageSourceCreateThumbnailAtIndex to create a thumbnail from an UIImage. All I have is the UIImage itself. The image is the snapshot on a UIView.

请,我不想使用任何其他方法来创建缩略图,仅使用 CGImageSourceCreateThumbnailAtIndex 即可,因为我想将其性能与已有的其他方法进行比较.

Please, I don't want to use any other method to create the thumbnail, just the one using CGImageSourceCreateThumbnailAtIndex, because I want to compare its performance with the other methods I already have.

说,这就是我到目前为止的代码.

Said that, this is the code I have so far.

我已经使用以下代码创建了 UIImage 类别:

I have create a UIImage category with this code:

- (UIImage *)createSquaredThumbnailWithWidth:(NSInteger)width {

  CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                         (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                         (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                         (id) kCGImageSourceThumbnailMaxPixelSize : @(width)
                                                         };

  CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);


  UIImage* scaled = [UIImage imageWithCGImage:scaledImageRef];

  CGImageRelease(scaledImageRef);

  return scaled;
}

我的问题是这条线:

  CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);

此函数的第一个参数需要一个 CGImageSourceRef ,但是就像我说的,我只有一个 UIImage ,该图像位于内存中,而不位于磁盘上,我不想将其保存到磁盘,否则性能会降低.

The first parameter of this function demands a CGImageSourceRef, but like I said, I just have an UIImage, that image is on memory, not on disk, and I don't want to save it to disk, or the performance will go down the drain.

如何从内存中的 UIImage 获取 CGImageSourceRef ?

推荐答案

您是否尝试过使用 CGImageSourceCreateWithData 并像这样将图像数据作为 CFDataRef 传递.

Have you try using CGImageSourceCreateWithData and passing image data as CFDataRef like this.

NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                     (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                     (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                     (id) kCGImageSourceThumbnailMaxPixelSize : @(width)
                                                     };

CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
UIImage *scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;

注意:如果您具有图像的 URL ,则可以使用 CGImageSourceCreateWithURL 创建 CGImageSourceRef .

Note: If you have URL of image then you can create CGImageSourceRef using CGImageSourceCreateWithURL.

CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL);

这篇关于使用CGImageSourceCreateThumbnailAtIndex从UIImage创建缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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