如何获取UIImagePickerController选择的图像文件大小?

时间:2021-02-22 18:21:09

I want to know that the image file size in IPhone PhotoAlbum which selected by UIImagePickerController.

我想知道UIImagePickerController选择的IPhone PhotoAlbum中的图像文件大小。

I've tried this code with 1,571,299 byte jpeg image.

我用1,571,299字节的jpeg图像试过这段代码。

UIIamge *selectedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

NSData *imageData; 
if ( /* PNG IMAGE */ ) 
    imageData = UIImagePNGReprensentation(selectedImage);
else 
    imageData = UIImageJPEGReprensentation(selectedImage);

NSUInteger fileLength = [imageData length];

NSLog(@"file length : [%u]", fileLength);

But when I run the code, it print 362788 byte.

但是当我运行代码时,它打印362788字节。

Is there anybody who know this?

有谁知道这个?

3 个解决方案

#1


5  

If you have code like this to take a picture:

如果您有这样的代码来拍照:

UIImagePickerController *controller = [[[UIImagePickerController alloc] init] autorelease];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.delegate = self;
[self presentModalViewController:controller animated:YES];

Then you can retrieve a file size of the picked image in the following way:

然后,您可以通过以下方式检索所选图像的文件大小:

NSURL *assetURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
    NSLog(@"Size: %lld", asset.defaultRepresentation.size);
} failureBlock:nil];

If the source type is UIImagePickerControllerSourceTypeCamera, you must save the in-memory image to disk before retrieving its file size.

如果源类型是UIImagePickerControllerSourceTypeCamera,则必须在检索文件大小之前将内存映像保存到磁盘。

#2


2  

As some commenters have said, even if we assume the methodology is correct you are reprocessing the image anyway so the byte sizes will not match. I use the following method for JPG images, ymmv for PNG:

正如一些评论者所说,即使我们假设方法是正确的,你仍然要重新处理图像,所以字节大小不匹配。我对JPG图像使用以下方法,对PNG使用ymmv:

+ (NSInteger)bytesInImage:(UIImage *)image {
    CGImageRef imageRef = [image CGImage];
    return CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);    
}

The above, as a commenter noted, does return the uncompressed size however.

正如评论者指出的那样,上述内容确实会返回未压缩的大小。

#3


0  

here is in Swift 4

这是在Swift 4中

extension UIImage {

    var memorySize: Int {
        guard let imageRef = self.cgImage else { return 0 }
        return imageRef.bytesPerRow * imageRef.height
    }
}

#1


5  

If you have code like this to take a picture:

如果您有这样的代码来拍照:

UIImagePickerController *controller = [[[UIImagePickerController alloc] init] autorelease];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.delegate = self;
[self presentModalViewController:controller animated:YES];

Then you can retrieve a file size of the picked image in the following way:

然后,您可以通过以下方式检索所选图像的文件大小:

NSURL *assetURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
    NSLog(@"Size: %lld", asset.defaultRepresentation.size);
} failureBlock:nil];

If the source type is UIImagePickerControllerSourceTypeCamera, you must save the in-memory image to disk before retrieving its file size.

如果源类型是UIImagePickerControllerSourceTypeCamera,则必须在检索文件大小之前将内存映像保存到磁盘。

#2


2  

As some commenters have said, even if we assume the methodology is correct you are reprocessing the image anyway so the byte sizes will not match. I use the following method for JPG images, ymmv for PNG:

正如一些评论者所说,即使我们假设方法是正确的,你仍然要重新处理图像,所以字节大小不匹配。我对JPG图像使用以下方法,对PNG使用ymmv:

+ (NSInteger)bytesInImage:(UIImage *)image {
    CGImageRef imageRef = [image CGImage];
    return CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);    
}

The above, as a commenter noted, does return the uncompressed size however.

正如评论者指出的那样,上述内容确实会返回未压缩的大小。

#3


0  

here is in Swift 4

这是在Swift 4中

extension UIImage {

    var memorySize: Int {
        guard let imageRef = self.cgImage else { return 0 }
        return imageRef.bytesPerRow * imageRef.height
    }
}