有没有办法从UIImage获取KB格式的文件大小,而没有从didFinishPickingMediaWithInfo获取该图像?呈现的图像来自相册。
我尝试了以下代码,但这会产生以下结果:以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;
}
答案 0 :(得分:4)
以下是计算HomeDirectory或Documents中文件大小的示例:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"yourimagename.png"]
文件sie计算:
filesize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];
NSLog(@"%lld",filesize);
在您添加文件大小之前,可以将其添加到.m文件
中@interface ViewController () {
long long filesize;
}
这将导致字节,如果您尝试将这些字节转换为kb,则可以使用NSByteCountFormatter,它将为您处理所有数学:
NSByteCountFormatter *sizeFormatter = [[NSByteCountFormatter alloc] init];
sizeFormatter.countStyle = NSByteCountFormatterCountStyleFile;
然后像这样调用它:
[sizeFormatter stringFromByteCount:filesize]
如果图像未保存在磁盘上,您可以通过这种方式计算尺寸:
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
答案 1 :(得分:1)
initWithFrame:
在调用setImage:
之前运行,因此在您进行计算时_image
为零。您可以将它们移动到setImage:
函数...
然而,这是一种测量图像大小的奇怪方法。 JPEG文件以及最终存储在图形内存中的内容有很大差异,因此,如果您是出于分析原因而进行此操作,则无法为您提供任何准确的测量结果。如果您只想要磁盘上的大小,只需通过NSFileManager
检查即可。
答案 2 :(得分:0)
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<include file="db.changelog.initial-state.xml" />
<changeSet author="captain" id="diff1">
do some stuff
</changeset>
<changeSet author="captain" id="diff2">
do some more stuff
</changeset>
...