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

发布于 2024-08-14 15:07:06 字数 118 浏览 2 评论 0原文

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

有什么想法吗?

I need to change the size of the cropping rect when using UIImagePickerController. In my case, I need the user to select images that are 320x385 but the crop rect currently only allows 320x320 (when allow editing is on).

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北陌 2024-08-21 15:07:06

我做过类似的项目。您必须使用图像控制器。在图像控制器上方,您必须添加另一个图像控制器(只是因为这是您的裁剪区域)
上面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);

I have done a similar project. You have to use an image controller. Above your image controller you have to add another image controller (just because this is your crop area)
aboveImageController's unfilled/empty part is where you will take. (so you should have an image with 320x480 sized and 320x385 empty in it.)

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);
无敌元气妹 2024-08-21 15:07:06

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

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

我假设,您一直在使用几乎没用的 EditedImage ...也许对于某些缩略图...无论如何,信息字典包含 CropRect 数据,这是您唯一需要做的事情是重新计算尺寸并自行裁剪 OriginalImage,例如使用:
UIImage:调整大小,然后裁剪

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

The dictionary in the delegate method returns all that:

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

I assume, you've been using the EditedImage which is pretty much useless ... maybe for some thumbnails ... anyway, the info dictionary contains CropRect data, only thing you have to do is to recalculate the sizes and crop the OriginalImage yourself, using for example this:
UIImage: Resize, then Crop

I haven't tested, this is just theory :) ... but in theory, this should work :D

扎心 2024-08-21 15:07:06

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

After getting image from image picker and give crop functionality as different module. So user can select the part of image which one they want to crop.

清晨说晚安 2024-08-21 15:07:06

你可以用这个

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;

}

You can use this

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文