简体   繁体   English

如何在不减小ios的高度和宽度的情况下减小图像的大小?

[英]how to reduce the size of image with out reducing height and width in ios?

Please guide me. 请指导我。 how to reduce the size of image with out reducing height and width in ios? 如何在不减小ios的高度和宽度的情况下减小图像的大小?

I will take a photo through device camera. 我将通过设备相机拍照。 the size around 250kb. 大小约为250kb。 Now I want to reduce 100KB without resizing the height and width. 现在,我想减少100KB而不调整高度和宽度。

Is it possible to do it? 有可能做到吗?

Please Share your views. 请分享您的看法。

Convert the jpg image to jpeg will reduce size. 将jpg图像转换为jpeg将减小尺寸。 Try this. 尝试这个。

[UIImage imageWithData:UIImageJPEGRepresentation(img, 0.01f)];

I generally use this function for compression of images on iOS that I found on this answer . 我通常将此功能用于在此答案中找到的iOS上的图像压缩。

+ (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. 图像类型可以是kUTTypePNG,但是质量参数很可能不起作用。 It also can be kUTTypeJPEG2000 for your needs. 根据您的需要,它也可以是kUTTypeJPEG2000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM