使用UIImagePickerController
时,我需要更改裁剪矩形的大小。在我的情况下,我需要用户选择320x385的图像,但裁剪矩形目前只允许320x320(当允许编辑时)。
有什么想法吗?
答案 0 :(得分:3)
我做了类似的项目。您必须使用图像控制器。在图像控制器上方,您必须添加另一个图像控制器(因为这是您的裁剪区域) aboveImageController的未填充/空白部分是您要拍摄的地方。 (所以你应该有一个尺寸为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);
答案 1 :(得分:2)
查看GKImagePicker
https://github.com/gekitz/GKImagePicker
示例项目只有iphone,只支持相册,但我很容易将它扩展到包括ipad和相机。我用它来制作一个自定义裁剪视图,它是一个任何尺寸大小的圆角矩形。
答案 2 :(得分:0)
查看this blog post,它为iPhone OS 2.x&提供了代码。 3.0
答案 3 :(得分:0)
委托方法中的字典返回所有内容:
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: Resize, then Crop
我还没有测试过,这只是理论:)但从理论上讲,这应该有效:D
答案 4 :(得分:0)
从图像选择器获取图像并将裁剪功能作为不同的模块。因此,用户可以选择要裁剪的图像部分。
答案 5 :(得分:0)
您可以使用此
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;
}