【问题标题】:How to get the compressed file size of an image received from `UIImagePickerController`?如何获取从 `UIImagePickerController` 接收到的图像的压缩文件大小?
【发布时间】:2013-02-19 20:28:02
【问题描述】:

我想知道UIImagePickerController通过camara或library拍摄的图像大小。有什么办法可以找到吗?

需求是这样的,

如果图像大小超过 1 MB,我想压缩它。

提前致谢。

【问题讨论】:

    标签: ios


    【解决方案1】:

    试试这个:

    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];

    【讨论】:

      【解决方案2】:

      如果你有路径可以询问文件管理器:

      attributesOfItemAtPath:error:
      

      键 'NSFileSize' 是您在字典中查找的内容。

      【讨论】:

        【解决方案3】:

        愿此代码对您有用。它不会为您提供字节或 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
        

        【讨论】:

          【解决方案4】:

          如果你有像 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;
          }
          

          【讨论】:

            猜你喜欢
            • 2016-02-08
            • 1970-01-01
            • 1970-01-01
            • 2018-08-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-29
            相关资源
            最近更新 更多