iOS 从 nsdata 压缩图像

发布于 2024-12-04 23:28:21 字数 178 浏览 0 评论 0原文

我有一个下载 jpeg 图像的应用程序,但它们很大,大于应用程序所需的大小。我创建了一个类来下载图像的 NSData。获取 NSData 并压缩图像的最佳方法是什么?

到目前为止,我见过的唯一途径是将图像临时写入磁盘,访问它并压缩磁盘上的 jpeg。

有没有一种替代方法,您不必写入磁盘并直接压缩它并接收数据?

I have an application that downloads jpeg images, but they're large, larger than necessary for the application. I've created a class that'll download the NSData of the image. What's the best way to take the NSData and compress the image.

So far the only route i've seen is to write the image to disk temporarily, access it and compress the jpeg on disk.

Is there an alternative way where you don't have to write to disk and compress it directly and the data is received?

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

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

发布评论

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

评论(2

笑梦风尘 2024-12-11 23:28:21

最好在应用程序下载图像之前对其进行压缩。但是,如果这超出了您的控制范围,请稍后压缩它们。

要将图像转换为另一种格式,您可以使用 CGImageDestination.h 中定义的函数

,我将创建一个转换给定图像的类方法函数:

+ (NSData*)imageDataWithCGImage:(CGImageRef)CGimage UTType:(const CFStringRef)imageUTType desiredCompressionQuality:(CGFloat)desiredCompressionQuality
{
    NSData* result = nil;

    CFMutableDataRef destinationData = CFDataCreateMutable(kCFAllocatorDefault, 0);
    CGImageDestinationRef destinationRef = CGImageDestinationCreateWithData(destinationData, imageUTType, 1, NULL);
    CGImageDestinationSetProperties(destinationRef, (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:desiredCompressionQuality], (NSString*)kCGImageDestinationLossyCompressionQuality, nil]);
    if (destinationRef != NULL)
    {
        CGImageDestinationAddImage(destinationRef, CGimage, NULL);

        if (CGImageDestinationFinalize(destinationRef))
        {
            result = [NSData dataWithData:(NSData*)destinationData];
        }
    }
    if (destinationData)
    {
        CFRelease(destinationData);
    }
    if (destinationRef)
    {
        CFRelease(destinationRef);
    }
    return result;
}

图像类型可以是 kUTTypePNG,但质量参数很可能不起作用。根据您的需要,它也可以是 kUTTypeJPEG2000。

您可以使用数据初始化新图像或将其保存到磁盘。

It would be better to compress the image before your application downloads it. However, if this is beyond your control, compress them after.

To convert an image to another format you can use functions defined in CGImageDestination.h

I would make a function of class method that converts a given image:

+ (NSData*)imageDataWithCGImage:(CGImageRef)CGimage UTType:(const CFStringRef)imageUTType desiredCompressionQuality:(CGFloat)desiredCompressionQuality
{
    NSData* result = nil;

    CFMutableDataRef destinationData = CFDataCreateMutable(kCFAllocatorDefault, 0);
    CGImageDestinationRef destinationRef = CGImageDestinationCreateWithData(destinationData, imageUTType, 1, NULL);
    CGImageDestinationSetProperties(destinationRef, (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:desiredCompressionQuality], (NSString*)kCGImageDestinationLossyCompressionQuality, nil]);
    if (destinationRef != NULL)
    {
        CGImageDestinationAddImage(destinationRef, CGimage, NULL);

        if (CGImageDestinationFinalize(destinationRef))
        {
            result = [NSData dataWithData:(NSData*)destinationData];
        }
    }
    if (destinationData)
    {
        CFRelease(destinationData);
    }
    if (destinationRef)
    {
        CFRelease(destinationRef);
    }
    return result;
}

The image type can be kUTTypePNG, but the quality parameter most likely will not work. It also can be kUTTypeJPEG2000 for your needs.

You can initialise a new image with the data or save it to disk.

与酒说心事 2024-12-11 23:28:21

如果您问我认为您在问什么,您可以使用 +imageWithData: 使用 NSData 块创建 UIImage 对象。

NSData *imageData = // Get the image data here
UIImage *image = [UIImage imageWithData:data];

If you're asking what I think you're asking, you can create UIImage objects with a chunk of NSData using +imageWithData:.

NSData *imageData = // Get the image data here
UIImage *image = [UIImage imageWithData:data];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文