我写了以下用于裁剪椭圆形图像的代码。但没有得到预期的结果。原始图像大小为382x453。并在CGRectMake(50,100,100,150)进行日食。但是图像始终从原始图像的0,0位置进行裁剪。我在这段代码中做了一些事情。请帮帮我,我错了。非常感谢,Ghufran
CGSize croppedSize = CGSizeMake(100, 150);
CGRect clippedRect = CGRectMake(0, 0, croppedSize.width, croppedSize.height);
UIGraphicsBeginImageContext(croppedSize);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextAddEllipseInRect(UIGraphicsGetCurrentContext(), clippedRect);
CGContextClosePath(UIGraphicsGetCurrentContext());
CGContextClip(UIGraphicsGetCurrentContext());
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, croppedSize.height);
CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(clippedRect.origin.x * -1, clippedRect.origin.y * -1, mImage.size.width, mImage.size.height), mImage.CGImage);
//pull the image from our cropped context
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
答案 0 :(得分:0)
我认为原点(50,100)需要反映在clippedRect的rect make中。
CGRect clippedRect = CGRectMake(0, 0, croppedSize.width, croppedSize.height);
变为
CGRect clippedRect = CGRectMake(50, 100, croppedSize.width, croppedSize.height);