UIImagePickerController编辑允许锁定UIImage的最大大小为320x320 [英] UIImagePickerController editing allowed locks UIImage to max size of 320x320

查看:130
本文介绍了UIImagePickerController编辑允许锁定UIImage的最大大小为320x320的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

使用iPhone OS 3.0+,整个UIImagePickerController API已更改。这个问题和答案应该考虑2.2。

With iPhone OS 3.0+, the whole UIImagePickerController API has changed. This question and answer should be considered 2.2. legacy code.

使用UIImagePickerController时,您允许编辑图像。 iPhone允许用户调整大小和平移图像。但是,编辑过的图片的最大尺寸上限为320x320。

When using the UIImagePickerController and you allow editing of the image. The iPhone allows the user to resize and pan the image. However, the max size of an edited image is capped at 320x320.

例如,我拍了一张iPhone屏幕截图,并把它放在照片库中,这是一个480x320 png。当我使用UIImagePickerController选择该图像时,即使我没有缩放或平移图像,它被裁剪为320x320,然后从UIImagePickerController返回。

As an example, I took an iPhone screenshot and placed it in the photo library, which is a 480x320 png. When I use a UIImagePickerController to select that image, even if I do NOT scale or pan the image, it is cropped to 320x320 before it is returned from the UIImagePickerController. However, if I turn editing off, the image is returned the proper 480x320 size.

我的理论:
非常巧妙的是,iPhone显示了2个非标准的半透明工具栏覆盖在图像上。这些工具栏在照片上留下一个无毒的320x320窗口。

My theory: Very subtly, the iPhone displays 2 nonstandard translucent tool bars that overlay over the image. These toolbars leave an innocuous 320x320 "window" over the photo. It appears to me that this window effectively clips the underlying photo.

注意:
回调也返回一个带有原始图像和剪切矩形的编辑字典,

Note: The callback also returns an editing dictionary with the original image and the clipping rect, but of course the rect is also max 320x320.

有关如何允许大于320x320的图片可以缩放和平移的任何想法?

Any ideas on how to allow images larger than 320x320 to be scaled and panned?

一些代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {

        self.myImageView.userInteractionEnabled=YES;
        CGRect imageFrame = myImageView.frame;
        CGPoint imageCenter = myImageView.center;
        imageFrame.size = img.size;
        myImageView.frame = imageFrame;
        self.myImageView.image = img;
        myImageView.center = imageCenter;

        [self dismissModalViewControllerAnimated:YES];
        [self performSelector:@selector(hideToolBars) withObject:nil afterDelay:2.0];
    }


推荐答案

一个问题在开发论坛和苹果定期讨论板。然而,我确实找到了一个办法。我使用了一些代码:

As craig said, this is an issue in the dev forums and apples regular discussion board. I did, however, find a way around it. I'm using a bit of code from:

苹果开发论坛

这包括你需要的大部分,并照顾所有的相机方向问题。我添加了以下内容,将采取编辑信息,并使用它来获得原始的裁剪rect与此添加:

This includes most of what you need, and takes care of all the camera orientation issues. I've added the following which will take in the editing info and use it to get the original cropping rect with this addition:

- (UIImage*)scaleImage:(UIImage*)anImage withEditingInfo:(NSDictionary*)editInfo{

    UIImage *newImage;

    UIImage *originalImage = [editInfo valueForKey:@"UIImagePickerControllerOriginalImage"];
    CGSize originalSize = CGSizeMake(originalImage.size.width, originalImage.size.height);
    CGRect originalFrame;
    originalFrame.origin = CGPointMake(0,0);
    originalFrame.size = originalSize;

    CGRect croppingRect = [[editInfo valueForKey:@"UIImagePickerControllerCropRect"] CGRectValue];
    CGSize croppingRectSize = CGSizeMake(croppingRect.size.width, croppingRect.size.height);

    CGSize croppedScaledImageSize = anImage.size;

    float scaledBarClipHeight = 80;

    CGSize scaledImageSize;
    float scale;

    if(!CGSizeEqualToSize(croppedScaledImageSize, originalSize)){

        scale = croppedScaledImageSize.width/croppingRectSize.width;
        float barClipHeight = scaledBarClipHeight/scale;

        croppingRect.origin.y -= barClipHeight;
        croppingRect.size.height += (2*barClipHeight);

        if(croppingRect.origin.y<=0){
            croppingRect.size.height += croppingRect.origin.y;
            croppingRect.origin.y=0;
        }

        if(croppingRect.size.height > (originalSize.height - croppingRect.origin.y)){
            croppingRect.size.height = (originalSize.height - croppingRect.origin.y);
        }


        scaledImageSize = croppingRect.size;
        scaledImageSize.width *= scale;
        scaledImageSize.height *= scale;

        newImage =  [self cropImage:originalImage to:croppingRect andScaleTo:scaledImageSize];

    }else{

        newImage = originalImage;

    }

    return newImage;
}

我将回调方法从dev论坛帖子更新为以下内容: / p>

I updated the call back method from the dev forums post to the following:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {

    [self dismissModalViewControllerAnimated:YES];
    self.myImageView.userInteractionEnabled=YES;
    CGRect imageFrame = myImageView.frame;
    CGPoint imageCenter = myImageView.center;
    UIImage *croppedImage;


    NSMutableDictionary *imageDescriptor = [editInfo mutableCopy];

    // CGFloat scaleSize = 400.0f;
    CGFloat scaleSize = 640.0f;
    switch ([picker sourceType]) {
            //done
        case UIImagePickerControllerSourceTypePhotoLibrary:
            croppedImage = [self scaleImage:img withEditingInfo:editInfo];
            [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
            break;


        case UIImagePickerControllerSourceTypeCamera: {
            UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
            if (originalOrientation != UIImageOrientationUp) {
                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                CGRect origRect;
                [[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
                UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
                CGFloat scale = scaleSize/1600.0f;
                origRect.origin.x *= scale;
                origRect.origin.y *= scale;
                origRect.size.width *= scale;
                origRect.size.height *= scale;
                croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
                [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
                [pool drain];
            }
            else {
                croppedImage = [self scaleImage:img withEditingInfo:editInfo];
                [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
            }
        }
            break;

        case UIImagePickerControllerSourceTypeSavedPhotosAlbum: {
            UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
            if (originalOrientation != UIImageOrientationUp) {
                NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
                CGRect origRect;
                [[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
                UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
                CGFloat scale = scaleSize/640.0f;
                origRect.origin.x *= scale;
                origRect.origin.y *= scale;
                origRect.size.width *= scale;
                origRect.size.height *= scale;
                croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
                [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
                [pool drain];
            }
            else {
                croppedImage = [self scaleImage:img withEditingInfo:editInfo];
                [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
            }
        }
            break;
        default:
            break;
    }

    imageFrame.size = croppedImage.size;
    myImageView.frame = imageFrame;
    myImageView.image = [imageDescriptor objectForKey:@"croppedImage"];
    myImageView.center = imageCenter;


}

这篇关于UIImagePickerController编辑允许锁定UIImage的最大大小为320x320的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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