簡體   English   中英

如何獲取從`UIImagePickerController`收到的圖像的壓縮文件大小?

[英]How to get the compressed file size of an image received from `UIImagePickerController`?

我想通過camara或庫知道UIImagePickerController拍攝的圖像大小。 有沒有辦法找到它?

要求就像,

如果圖像大小超過1 MB,我想壓縮它。

提前致謝。

嘗試這個:

CGFloat compression = 0.8f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 1024;
UIImage *img =[UIImage imageNamed:@"anyimg.jpg"];
NSData *imageData = UIImageJPEGRepresentation(img, compression);

while ([imageData length] > maxFileSize && compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(img, compression);
    NSLog(@"%d",[imageData length]);
}
NSLog(@"image size %d",[imageData length]);

獲取圖像大小:
NSData *data_size = UIImagePNGRepresentation(yourImage);
int image_size =[data_size length];

如果你有一個路徑,你可以問文件管理器:

attributesOfItemAtPath:error:

關鍵詞“NSFileSize”就是您在字典中尋找的內容。

願這段代碼適合您。 它沒有像字節或mb那樣給你大小的圖像。 但是此代碼會根據您的自定義大小(如100x200)為您提供圖像。

@implementation UIImage (CS_Extensions)
    - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {

        UIImage *sourceImage = self;
        UIImage *newImage = nil;

        CGSize imageSize = sourceImage.size;
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;

        CGFloat targetWidth = targetSize.width;
        CGFloat targetHeight = targetSize.height;

        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth = targetWidth;
        CGFloat scaledHeight = targetHeight;

        CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

        if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;

            if (widthFactor < heightFactor)
                scaleFactor = widthFactor;
            else
                scaleFactor = heightFactor;

            scaledWidth  = width * scaleFactor;
            scaledHeight = height * scaleFactor;

            // center the image

            if (widthFactor < heightFactor) {
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            } else if (widthFactor > heightFactor) {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }


        // this is actually the interesting part:

        UIGraphicsBeginImageContext(targetSize);

        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width  = scaledWidth;
        thumbnailRect.size.height = scaledHeight;

        [sourceImage drawInRect:thumbnailRect];

        newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        if(newImage == nil) NSLog(@"could not scale image");


        return newImage ;
    }
    @end

如果你有像jinx這樣的道路,你可以這樣做

NSNumber *theFileSize = [NSNumber numberWithInt:0];
NSFileManager * filemanager = [NSFileManager defaultManager];

if([filemanager fileExistsAtPath:fullFilePath])
{
    NSDictionary * attributes = [filemanager attributesOfItemAtPath:fullFilePath error:nil];
    theFileSize = [attributes objectForKey:NSFileSize];
}

[self convertbyteToKB_MB:[theFileSize  intValue];

...

-(NSString*)convertbyteToKB_MB:(int)byte 
{
NSString    *retSize = [NSString stringWithFormat:@"%d bytes",byte];
float       fByte = byte;
if (byte > 1024)
{
    //Kilobytes
    fByte = fByte / 1024;
    retSize = [NSString stringWithFormat:@"%.1f KB",fByte];
}

if (fByte > 1024)
{
    //Megabytes
    fByte = fByte / 1024;
    retSize = [NSString stringWithFormat:@"%.1f MB",fByte]; 
}

return retSize;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM