簡體   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