iOS查看图片大小的方法有两种:使用UIImage的size属性
iOS查看图片大小的方法有两种:
1. 使用UIImage的size属性
可以使用UIImage的size属性来查看图片的大小。
Objective-C代码:
UIImage *image = [UIImage imageNamed:@"imageName"];
CGSize size = image.size;
NSLog(@"image size is %f, %f", size.width, size.height);
Swift代码:
let image = UIImage(named: "imageName")
let size = image.size
print("image size is \(size.width), \(size.height)")
2. 使用NSFileManager的attributesOfItemAtPath方法
也可以使用NSFileManager的attributesOfItemAtPath方法来查看图片的大小。
Objective-C代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSNumber *fileSize = [fileAttributes objectForKey:NSFileSize];
NSLog(@"image size is %@", fileSize);
Swift代码:
let filePath = Bundle.main.path(forResource: "imageName", ofType: "png")
let fileAttributes = try? FileManager.default.attributesOfItem(atPath: filePath!)
let fileSize = fileAttributes![FileAttributeKey.size] as! NSNumber
print("image size is \(fileSize)")
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(34条)