我需要获得扩展包的大小。为此,我有这段代码:
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
UIButton *backPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backPageButton setFrame:CGRectMake(0, 0, 50, 30)];
[ backPageButton setImage:[UIImage imageNamed:@"left_arrow.png"] forState:UIControlStateNormal];
backPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[backPageButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
[backPageButton setUserInteractionEnabled:YES];
[container addSubview:backPageButton];
UIButton *forwardPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[forwardPageButton setFrame:CGRectMake(30, 0, 50, 30)];
[ forwardPageButton setImage:[UIImage imageNamed:@"right_arrow.png"] forState:UIControlStateNormal];
[forwardPageButton addTarget:self action:@selector(forwardButtonAction) forControlEvents:UIControlEventTouchUpInside];
forwardPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[forwardPageButton setUserInteractionEnabled:YES];
[container addSubview:forwardPageButton];
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;
问题是它总是4096.总是。这是一个错误,还是我错过了什么?
答案 0 :(得分:1)
您必须枚举包中的所有文件并计算尺寸,这是一个在后台/活动页面中有效的示例:
function calcPackageSize(callback) {
var totalSize = 0;
var queue = [], xhr = new XMLHttpRequest();
xhr.responseType = "blob";
xhr.onload = function() {
totalSize += this.response.size;
calcFile();
};
chrome.runtime.getPackageDirectoryEntry(function(root) {
var rootDirNameLength = root.fullPath.length;
calcDir(root);
function calcDir(dir) {
dir.createReader().readEntries(function(entries) {
entries.forEach(function(entry) {
if (entry.isFile) {
queue.push(entry.fullPath.substr(rootDirNameLength));
!xhr.readyState && calcFile(); // start the request chain
} else {
calcDir(entry);
}
});
});
}
});
function calcFile() {
if (!queue.length)
return callback && callback(totalSize);
xhr.open("HEAD", queue.pop());
xhr.send();
}
}
用法:
calcPackageSize(function(size) { console.log(size) });