使用IOS应用程序将图像上传到服务器文件大小太大

使用IOS应用程序将图像上传到服务器文件大小太大

问题描述:

我正在使用IOS应用将照片上传到服务器。重要的是照片上传时不会有任何质量损失,并以jpeg的形式上传。我目前的问题是上传的照片没有质量损失,但文件大小超过预期。例如:我通过应用程序上传了一个文件,文件大小为4.7 MB。当我通过电子邮件发送同一张照片给自己并为电子邮件选择实际照片选项时,照片的大小仅为1.7 MB。并排比较显示质量没有差异。

I am uploading photos to a server with an IOS app. It is important that the photos are uploaded with no loss in quality and are uploaded as jpeg's. My current problem is that the photos upload with no loss of quality but have a larger than expected file size. For example: I uploaded a file through the app and the file size was 4.7 MB. When I Emailed the same photo to myself and selected the "Actual Photo" option for the email, The size of the photo was only 1.7 MB. A side by side comparison revealed no difference in quality.

以下是我上传文件的方式。

Here is how I am uploading the files.

ALAssetsLibrary *library = [ALAssetsLibrary new];
[library getImageAtURL:orderImage.imageUrl with completionBlock:^(UIImage *image)

NSData *fileData = UIImageJPEGRepresentation(image, 1.0)

NSURLRequest *request = [self multipartFormRequestWithMethod:@"POST" path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
    [formData appendPartWithFileData:fileData name:@"uploadedfile" fileName:fileName mimeType:mimeType];
    [formData appendPartWithFormData:[extraInfo dataUsingEncoding:NSISOLatin2StringEncoding] name:@"extraInfo"];
}];


问题是 UIImageJPEGRepresentation 。它不会检索原始JPEG,而是创建一个新的JPEG。当你使用 compressionQuality 1 时(可能是为了避免进一步的图像质量损失),它会创建这个新的表示形式没有压缩(通常导致文件大于原始文件)。

The problem is UIImageJPEGRepresentation. It does not retrieve the original JPEG, but rather creates a new JPEG. And when you use a compressionQuality of 1 (presumably to avoid further image quality loss), it creates this new representation with no compression (generally resulting in a file larger than the original).

我建议使用 getBytes 来检索原始资产,而不是通过 UIImage 并通过获取数据UIImageJPEGRepresentation

I would advise using getBytes to retrieve the original asset, rather than round-tripping it through a UIImage and getting the data via UIImageJPEGRepresentation:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetsLibraryURL resultBlock:^(ALAsset *asset) {
    ALAssetRepresentation *representation = [asset defaultRepresentation];

    // I generally would write directly to a `NSOutputStream`, but if you want it in a
    // NSData, it would be something like:

    NSMutableData *data = [NSMutableData data];

    // now loop, reading data into buffer and writing that to our data stream

    NSError *error;
    long long bufferOffset = 0ll;
    NSInteger bufferSize = 10000;
    long long bytesRemaining = [representation size];
    uint8_t buffer[bufferSize];
    while (bytesRemaining > 0) {
        NSUInteger bytesRead = [representation getBytes:buffer fromOffset:bufferOffset length:bufferSize error:&error];
        if (bytesRead == 0) {
            NSLog(@"error reading asset representation: %@", error);
            return;
        }
        bytesRemaining -= bytesRead;
        bufferOffset   += bytesRead;
        [data appendBytes:buffer length:bytesRead];
    }

    // ok, successfully read original asset; 
    // do whatever you want with it here

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

-

如果你是使用iOS 8中引入的Photos框架,可以使用 PHImageManager 来获取图像数据:

If you're using the Photos framework introduced in iOS 8, can use PHImageManager to get the image data:

PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[assetsLibraryURL] options:nil];
PHAsset *asset = [result firstObject];
if (asset) {
    PHImageManager *manager = [PHImageManager defaultManager];
    [manager requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
        // use `imageData` here
    }];
}