Ios 从iphone摄像头保存和裁剪UIImage
我有以下代码:Ios 从iphone摄像头保存和裁剪UIImage,ios,ios7,uiimage,cgimageref,uigraphicscontext,Ios,Ios7,Uiimage,Cgimageref,Uigraphicscontext,我有以下代码: float photoWidth = photo.size.width; float photoHeight = photo.size.height; UIImage *picture = nil; if (photoWidth < photoHeight) { // Portrait // Scale the photo down CGRect rect = CGRectMake
float photoWidth = photo.size.width;
float photoHeight = photo.size.height;
UIImage *picture = nil;
if (photoWidth < photoHeight)
{
// Portrait
// Scale the photo down
CGRect rect = CGRectMake(0.0, 0.0, 450.0, 600.0);
UIGraphicsBeginImageContext( rect.size );
[photo drawInRect:rect];
picture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else
{
// Landscape
// Crop off the edges
float scale = photoHeight / photoWidth;
float newWidth = photoHeight * scale;
float newHeight = photoHeight;
float newX = (newHeight - newWidth) / 2.0;
float newY = 0.0;
CGRect cropped = CGRectMake(newX, newY, newWidth, newHeight);
CGImageRef imageRef = CGImageCreateWithImageInRect ([photo CGImage], cropped);
UIImage * croppedPhoto = [UIImage imageWithCGImage: imageRef];
CGImageRelease (imageRef);
// Scale the photo down
CGRect rect = CGRectMake(0.0, 0.0, 450.0, 600.0);
UIGraphicsBeginImageContext( rect.size );
[croppedPhoto drawInRect:rect];
picture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
NSData *photoData = UIImagePNGRepresentation(picture);
float photoWidth=photo.size.width;
浮动照片高度=photo.size.height;
UIImage*picture=nil;
if(照片宽度<照片高度)
{
//肖像画
//缩小照片的比例
CGRect rect=CGRectMake(0.0,0.0450.0600.0);
UIGraphicsBeginImageContext(rect.size);
[图片绘制目录:rect];
picture=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
}
其他的
{
//景观
//剪掉边缘
浮标=照片高度/照片宽度;
浮动宽度=照片高度*比例;
浮动新高度=照片高度;
float newX=(newHeight-newWidth)/2.0;
float newY=0.0;
CGRect CROPED=CGRectMake(newX,newY,newWidth,newHeight);
CGImageRef imageRef=CGImageCreateWithImageInRect([photo CGImage],裁剪);
UIImage*croppedPhoto=[UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
//缩小照片的比例
CGRect rect=CGRectMake(0.0,0.0450.0600.0);
UIGraphicsBeginImageContext(rect.size);
[截取的照片绘制:rect];
picture=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
}
NSData*photoData=UIImagePngResentation(图片);
当我使用photoData并将其转换为UIImage时,纵向模式工作正常。但在景观模式方面存在问题
我正试图通过裁剪照片的左右边缘,使风景与我的肖像大小相同
还注意到,如果我在拍照时翻转了横向相机,方向会使我的照片朝上向下
我这样做不对吗
谢谢。试着改变一下
[UIImage imageWithCGImage: imageRef];
到
或
我还是不能让它工作太久。取决于我如何握住横向相机,它仍然是颠倒的,第二个矩形是关闭的。
[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:UIImageOrientationUp];
[UIImage imageWithCGImage:imageRef scale: photo.scale orientation:photo.imageOrientation];