您好我正在使用UIimage,我需要将图像裁剪为特定尺寸,假设我的特定尺寸宽度等于设备宽度,高度固定为200。 我尝试了很多例子,但没有那些不适合我的人,我试过以下样本
reshape
以下是我的实际图片
裁剪后我会变得如下
我所有的例子都在拉伸图像但是我想要伸展需要缩放图像。我在哪里缺少?请帮助我。
答案 0 :(得分:0)
您正在使用的代码将调整图像大小,我们将其用于图像压缩或图像大小调整。 如果要裁剪图像,有两种方法。 1)允许用户在选择图像时进行编辑。 您可以通过将allowEditing属性添加到选择器(
)来实现此目的2016-12-23 22:23:38.060 Quiz App[9064:278885] -[Quiz_App.ViewController Btn3:]: unrecognized selector sent to instance 0x7ffce6507860
2016-12-23 22:23:38.084 Quiz App[9064:278885] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Quiz_App.ViewController Btn3:]: unrecognized selector sent to instance 0x7ffce6507860'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109600d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010691321e objc_exception_throw + 48
2 CoreFoundation 0x0000000109670f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x0000000109586005 ___forwarding___ + 1013
4 CoreFoundation 0x0000000109585b88 _CF_forwarding_prep_0 + 120
5 UIKit 0x0000000106de48bc -[UIApplication sendAction:to:from:forEvent:] + 83
6 UIKit 0x0000000106f6ac38 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x0000000106f6af51 -[UIControl _sendActionsForEvents:withEvent:] + 444
8 UIKit 0x0000000106f69e4d -[UIControl touchesEnded:withEvent:] + 668
9 UIKit 0x0000000106e52545 -[UIWindow _sendTouchesForEvent:] + 2747
10 UIKit 0x0000000106e53c33 -[UIWindow sendEvent:] + 4011
11 UIKit 0x0000000106e009ab -[UIApplication sendEvent:] + 371
12 UIKit 0x00000001075ed72d __dispatchPreprocessedEventFromEventQueue + 3248
13 UIKit 0x00000001075e6463 __handleEventQueue + 4879
14 CoreFoundation 0x00000001095a5761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x000000010958a98c __CFRunLoopDoSources0 + 556
16 CoreFoundation 0x0000000109589e76 __CFRunLoopRun + 918
17 CoreFoundation 0x0000000109589884 CFRunLoopRunSpecific + 420
18 GraphicsServices 0x000000010b540a6f GSEventRunModal + 161
19 UIKit 0x0000000106de2c68 UIApplicationMain + 159
20 Quiz App 0x000000010633040f main + 111
21 libdyld.dylib 0x000000010a5b068d start + 1
22 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
这将为您提供矩形裁剪图像,您可以从选择器视图的 didFinish 方法中检索已编辑的图像
2)通过编程
这是我正在使用的功能
pickerView.allowEditing = true
以上代码将返回裁剪后的图像。
答案 1 :(得分:0)
这些会将您的图像裁剪成您需要的尺寸。
- (CGImageRef)newTransformedImage:(CGAffineTransform)transform
sourceImage:(CGImageRef)sourceImage
sourceSize:(CGSize)sourceSize
sourceOrientation:(UIImageOrientation)sourceOrientation
outputWidth:(CGFloat)outputWidth
cropSize:(CGSize)cropSize
imageViewSize:(CGSize)imageViewSize
{
CGImageRef source = [self newScaledImage:sourceImage
withOrientation:sourceOrientation
toSize:sourceSize
withQuality:kCGInterpolationNone];
CGFloat aspect = cropSize.height/cropSize.width;
CGSize outputSize = CGSizeMake(outputWidth, outputWidth*aspect);
CGContextRef context = CGBitmapContextCreate(NULL,
outputSize.width,
outputSize.height,
CGImageGetBitsPerComponent(source),
0,
CGImageGetColorSpace(source),
CGImageGetBitmapInfo(source));
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, outputSize.width, outputSize.height));
CGAffineTransform uiCoords = CGAffineTransformMakeScale(outputSize.width / cropSize.width,
outputSize.height / cropSize.height);
uiCoords = CGAffineTransformTranslate(uiCoords, cropSize.width/2.0, cropSize.height / 2.0);
uiCoords = CGAffineTransformScale(uiCoords, 1.0, -1.0);
CGContextConcatCTM(context, uiCoords);
CGContextConcatCTM(context, transform);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(-imageViewSize.width/2.0,
-imageViewSize.height/2.0,
imageViewSize.width,
imageViewSize.height)
, source);
CGImageRef resultRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGImageRelease(source);
return resultRef;
}
- (CGImageRef)newScaledImage:(CGImageRef)source withOrientation:(UIImageOrientation)orientation toSize:(CGSize)size withQuality:(CGInterpolationQuality)quality
{
CGSize srcSize = size;
CGFloat rotation = 0.0;
switch(orientation)
{
case UIImageOrientationUp: {
rotation = 0;
} break;
case UIImageOrientationDown: {
rotation = M_PI;
} break;
case UIImageOrientationLeft:{
rotation = M_PI_2;
srcSize = CGSizeMake(size.height, size.width);
} break;
case UIImageOrientationRight: {
rotation = -M_PI_2;
srcSize = CGSizeMake(size.height, size.width);
} break;
default:
break;
}
CGContextRef context = CGBitmapContextCreate(NULL,
size.width,
size.height,
8, //CGImageGetBitsPerComponent(source),
0,
CGImageGetColorSpace(source),
(CGBitmapInfo)kCGImageAlphaNoneSkipFirst //CGImageGetBitmapInfo(source)
);
CGContextSetInterpolationQuality(context, quality);
CGContextTranslateCTM(context, size.width/2, size.height/2);
CGContextRotateCTM(context,rotation);
CGContextDrawImage(context, CGRectMake(-srcSize.width/2 ,
-srcSize.height/2,
srcSize.width,
srcSize.height),
source);
CGImageRef resultRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
return resultRef;
}
///你要裁剪的地方。
CGImageRef imageRef = [self newTransformedImage:transform
sourceImage:self.image.CGImage
sourceSize:self.image.size
sourceOrientation:self.image.imageOrientation
outputWidth:self.image.size.width
cropSize:self.photoView.cropView.frame.size
imageViewSize:self.photoView.photoContentView.bounds.size];
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
谢谢。享受编码:)