在 Xcode 中检查图像的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6410908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:18:23  来源:igfitidea点击:

check the size of image in Xcode

iphonexcode

提问by Sohan

When i copy some image files (PNG format only) into my app in Xcode, is the size of the image altered?

当我在 Xcode 中将一些图像文件(仅限 PNG 格式)复制到我的应用程序中时,图像的大小是否发生了变化?

Alternately, how can i check the size of my PNG image within xcode?

或者,如何在 xcode 中检查 PNG 图像的大小?

Thanks in advance!

提前致谢!

回答by Cyprian

Here is how you can print the size in bytes:

以下是打印字节大小的方法:

UIImage *myImage = [UIImage imageNamed:@"xxx.png"];

NSLog(@"MyImage size in bytes:%i",[UIImagePNGRepresentation(myImage) length]);

回答by Cocoanetics

PNG files are unchanged when you add them to your Xcode project. They are compressed with pngcrush at time of build. You can right-click on your .app result, and look at the package contents to see the final file sizes of the images.

当您将 PNG 文件添加到您的 Xcode 项目时,它们不会发生变化。它们在构建时用 pngcrush 压缩。您可以右键单击 .app 结果,然后查看包内容以查看图像的最终文件大小。

Or if you are not talking about file size, but pixel size then you can load the image into a UIImage and query its "size" property.

或者,如果您不是在谈论文件大小,而是在谈论像素大小,那么您可以将图像加载到 UIImage 中并查询其“大小”属性。

回答by gsempe

You create an UIImageView and then you print the frame of your new UIImageView

您创建一个 UIImageView,然后打印新 UIImageView 的框架

UIImageView imageView = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithData:/*image path */];
NSLog(@"photoLoaderDidLoad: self.frame %@",NSStringFromCGRect(imageView.frame));

回答by Darkngs

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editInfo{
   UIImage *image=[editInfo valueForKey:UIImagePickerControllerOriginalImage];
   NSURL *imageURL=[editInfo valueForKey:UIImagePickerControllerReferenceURL];
   __block long long realSize;

   ALAssetsLibraryAssetForURLResultBlock resultBlock=^(ALAsset *asset)
   {
      ALAssetRepresentation *representation=[asset defaultRepresentation];
      realSize=[representation size];
   };

   ALAssetsLibraryAccessFailureBlock failureBlock=^(NSError *error)
   {
      NSLog(@"%@", [error localizedDescription]);
   };

   if(imageURL)
   {
      ALAssetsLibrary *assetsLibrary=[[[ALAssetsLibrary alloc] init] autorelease];
      [assetsLibrary assetForURL:imageURL resultBlock:resultBlock failureBlock:failureBlock];
   }
}