Phonegap构建IOS camera.getpicture质量参数不工作 [英] Phonegap Build IOS camera.getpicture quality parameter not working

查看:227
本文介绍了Phonegap构建IOS camera.getpicture质量参数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Phonegap应用程序,并使用Build服务编译它。
我的应用程序在这个代码块中调用摄像头:



function capturePhoto(){

  //使用设备摄像头拍摄图片并检索图片
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{
quality:20,
destinationType:destinationType。 FILE_URI});

}

生成的映像通过ajax上传到服务器。
当我查看上传的图像,一些到达与他们的质量适当地减少,但有些到达与大约4 MB的文件大小。我已经意识到,在iPhone 4和5上运行我的应用程序的用户(我没有6个测试者)始终在上传大图片。



到10和5. Android提交的图像的图像大小适当减少,但IOS图像仍然很大。任何人都可以告诉我,如果有一个已知的问题在这里?



API文件表示您无法降低从图库或相机胶卷中选择的图片质量,但这些都不是。

解决方案

这是插件在拍摄照片后用于处理图片的代码:

   - (NSData *)processImage:(UIImage *)image info:(NSDictionary *)info options:(CDVPictureOptions *)options 
{
NSData * data = nil;

switch(options.encodingType){
case EncodingTypePNG:
data = UIImagePNGRepresentation(image);
break;
case EncodingTypeJPEG:
{
if((options.allowsEditing == NO)&&(options.targetSize.width< = 0)&&(options.targetSize。 height = <0)&(options.correctOrientation == NO)){
//根据请求使用未编辑的图片,不要调整大小
data = UIImageJPEGRepresentation(image,1.0);
} else {
if(options.usesGeolocation){
NSDictionary * controllerMetadata = [info objectForKey:@UIImagePickerControllerMediaMetadata];
if(controllerMetadata){
self.data = data;
self.metadata = [[NSMutableDictionary alloc] init];

NSMutableDictionary * EXIFDictionary = [[controllerMetadata objectForKey:(NSString *)kCGImagePropertyExifDictionary] mutableCopy];
if(EXIFDictionary){
[self.metadata setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
}

if(IsAtLeastiOSVersion(@8.0)){
[[self locationManager] performSelector:NSSelectorFromString(@requestWhenInUseAuthorization)withObject:nil afterDelay:0];
}
[[self locationManager] startUpdatingLocation];
}
} else {
data = UIImageJPEGRepresentation(image,[options.quality floatValue] / 100.0f);
}
}
}
break;
默认值:
break;
};

返回数据;
}

因此,如果 options.encodingType 是PNG的图像质量不变。



但是,请检查

  if((options.allowsEditing == NO)&&&(options.targetSize.width< = 0)&&(options.targetSize.height< = 0)&& ;(options.correctOrientation == NO))

allowEditing 默认为NO



targetSize.width targetSize.height 默认为0



correctOrientation

所有if的条件都满足,质量没有变化。



这听起来像是一个错误,因为它不是



那么,你现在可以做什么呢?



您可以在cordova jira页面 https://issues.apache上提出问题。 org / jira / browse / CB
和/或你可以传递任何参数来更改默认值,让代码跳过if,并转到使用质量代码的else。更改任何一个都应该工作:

  allowEdit:true,
targetWidth:100,
targetHeight:100 ,
correctOrientation:true

编辑:已经有一个错误打开
< a href =https://issues.apache.org/jira/browse/CB-6190 =nofollow> https://issues.apache.org/jira/browse/CB-6190



编辑2:我修复了问题,您可以通过 cordova插件添加https://github.com/apache/cordova-plugin -camera 或等待它发布到NPM


I have written a Phonegap application and compiled it using the Build service. My application calls the camera in this block of code:

function capturePhoto() {

        // Take picture using device camera and retrieve image 
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, { 
              quality: 20,
              destinationType: destinationType.FILE_URI });

    }

The resulting image is uploaded to a server via ajax. When I review the uploaded images, some arrive with their quality reduced appropriately but some arrive with a filesize of ~4 MB. I have realized that users who are running my app on iPhone 4 and 5 (I have no testers with 6 yet) are consistently uploading the large images.

I have reduced quality to 10 and 5. Image size of Android-submitted images are appropriately reduced- but still IOS images are large. Can anyone tell me if there is a known issue here?

The API Docs say that you cannot reduce quality of images selected from gallery or camera roll- but these are not. They are loaded from camera.

解决方案

This is the code the plugin uses for processing the image after taking the picture:

- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options
{
    NSData* data = nil;

    switch (options.encodingType) {
        case EncodingTypePNG:
            data = UIImagePNGRepresentation(image);
            break;
        case EncodingTypeJPEG:
        {
            if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO)){
                // use image unedited as requested , don't resize
                data = UIImageJPEGRepresentation(image, 1.0);
            } else {
                if (options.usesGeolocation) {
                    NSDictionary* controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
                    if (controllerMetadata) {
                        self.data = data;
                        self.metadata = [[NSMutableDictionary alloc] init];

                        NSMutableDictionary* EXIFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
                        if (EXIFDictionary) {
                            [self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
                        }

                        if (IsAtLeastiOSVersion(@"8.0")) {
                            [[self locationManager] performSelector:NSSelectorFromString(@"requestWhenInUseAuthorization") withObject:nil afterDelay:0];
                        }
                        [[self locationManager] startUpdatingLocation];
                    }
                } else {
                    data = UIImageJPEGRepresentation(image, [options.quality floatValue] / 100.0f);
                }
            }
        }
            break;
        default:
            break;
    };

    return data;
}

So, if options.encodingType is PNG the image quality isn't change. The default value is JPG, so the problem isn't there.

But then check the

if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO))

allowsEditing is NO by default

targetSize.width and targetSize.height are 0 by default

correctOrientation is NO by default

So all the conditions of the if are meet, and no change in quality is done.

It sounds like a bug as it isn't mentioned anywhere that you have to specify any other value to make the quality param work.

So, what can you do now?

You can file an issue on the cordova jira page https://issues.apache.org/jira/browse/CB And/or you can pass any param to change the default value and make the code to skip the if and go to the else with the quality code. Changing any of this should work:

  allowEdit : true,
  targetWidth: 100,
  targetHeight: 100,
  correctOrientation: true

EDIT: there is already a bug open https://issues.apache.org/jira/browse/CB-6190

EDIT 2: I fixed the issue, you can get the changes with cordova plugin add https://github.com/apache/cordova-plugin-camera or wait until it's released to NPM

这篇关于Phonegap构建IOS camera.getpicture质量参数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆