问题
Is there a way to get the filesize in KB from a UIImage, without getting that image from didFinishPickingMediaWithInfo? The images that are presented are coming from the photo album.
I tried the following code, but this gives the following result: size of image in KB: 0.000000
- (void)setImage:(UIImage *)image
{
_image = image;
self.imageView.image = image;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupView];
self.backgroundColor = [UIColor whiteColor];
panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(beingDragged:)];
[self addGestureRecognizer:panGestureRecognizer];
// prepare image view
self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
self.imageView.clipsToBounds = YES;
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:self.imageView];
NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((_image), 0.5)];
int imageSize = imgData.length;
NSLog(@"size of image in KB: %f ", imageSize/1024.0);
overlayView = [[OverlayView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-100, 0, 100, 100)];
overlayView.alpha = 0;
[self addSubview:overlayView];
}
return self;
}
回答1:
Here is an example of calculating file sizes of files in your HomeDirectory or Documents:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"yourimagename.png"]
File sie is calculated:
filesize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];
NSLog(@"%lld",filesize);
Before you do that add filesize, you can add it in the .m file
@interface ViewController () {
long long filesize;
}
this will result in bytes, if you are trying to convert those bytes into kb you can use the NSByteCountFormatter and it will take care of all the math for you:
NSByteCountFormatter *sizeFormatter = [[NSByteCountFormatter alloc] init];
sizeFormatter.countStyle = NSByteCountFormatterCountStyleFile;
and then call it like so :
[sizeFormatter stringFromByteCount:filesize]
If the image is not saved on the disk you can calculate the size this way:
NSData *imgData = UIImageJPEGRepresentation(_image, 1);
filesize = [imgData length]; //filesize in this case will be an int not long long so use %d to NSLog it
回答2:
initWithFrame:
runs before setImage:
is called, so _image
is nil at the time you are doing your calculations. You could move them into the setImage:
function...
However, this is a weird way of measuring the size of the image. A JPEG file and what ends up in graphics memory are widely different, so if you are doing it for profiling reasons, this is not going to give you any accurate measurements. If you just want the size on disk, you can simply check that through NSFileManager
.
回答3:
NSInteger imageDataSize = CGImageGetHeight(image.CGImage) * CGImageGetBytesPerRow(image.CGImage);
来源:https://stackoverflow.com/questions/27431709/how-to-get-the-size-of-a-uiimage-in-kb