繁体   English   中英

使用UIImagePickerController时如何更改裁剪矩形的大小

[英]How to change the size of the crop rect when using UIImagePickerController

使用UIImagePickerController时,我需要更改裁剪矩形的大小。 就我而言,我需要用户选择320x385的图像,但裁剪区域当前仅允许320x320(启用允许编辑时)。

有任何想法吗?

我做了一个类似的项目。 您必须使用图像控制器。 在图像控制器上方,您需要在ImageController的未填充/空部分上方添加另一个图像控制器(仅因为这是您的裁剪区域)。 (因此,您应该具有尺寸为320x480的图片,其中包含320x385的空白图片。)

UIImage *image=theimageView.image;  
CGSize newSize;
newSize.width=320;
newSize.height=385;
UIGraphicsBeginImageContext(newSize);

//x=0 (because widhth : 320) y=0 (aboveImageController's empty part's start) 
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef imageRef = CGImageCreateWithImageInRect([newImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

从图像选择器获取图像并将裁剪功能作为其他模块提供。 因此,用户可以选择他们想要裁剪的图像部分。

你可以用这个

UIImage *image1 = [self resizeImage:image width:320 height:385];


-(UIImage *)resizeImage:(UIImage *)image width:(int)wdth height:(int)hght{
    int w = image.size.width;
    int h = image.size.height;
    CGImageRef imageRef = [image CGImage];
    int width, height;
    int destWidth = wdth;
    int destHeight = hght;
    if(w > h){
        width = destWidth;
        height = h*destWidth/w;
    }
    else {
        height = destHeight;
        width = w*destHeight/h;
    }
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmap;
    bitmap = CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedFirst);

    if (image.imageOrientation == UIImageOrientationLeft) {

        CGContextRotateCTM (bitmap, M_PI/2);
        CGContextTranslateCTM (bitmap, 0, -height);

    } else if (image.imageOrientation == UIImageOrientationRight) {

        CGContextRotateCTM (bitmap, -M_PI/2);
        CGContextTranslateCTM (bitmap, -width, 0);

    }
    else if (image.imageOrientation == UIImageOrientationUp) {

    } else if (image.imageOrientation == UIImageOrientationDown) {

        CGContextTranslateCTM (bitmap, width,height);
        CGContextRotateCTM (bitmap, -M_PI);
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];
    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;

}

委托方法中的字典返回所有内容:

NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;

我认为,您一直在使用EditedImage,这几乎是无用的……也许对于某些缩略图……无论如何,信息字典中包含CropRect数据,唯一要做的就是重新计算大小并自己裁剪原始Image ,例如使用: UIImage:调整大小,然后裁剪

我还没有测试过,这只是理论上的:) ...但是从理论上讲,这应该可行:D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM