从UIImage创建矩形

时间:2016-07-06 02:13:02

标签: ios objective-c uiimage

我想知道是否可以从下面的图像创建一个矩形来消除奇怪的角落:

Rectangle

输入图片:

<UIImage>, {1334, 955}

这是我到目前为止所做的:

int croppingNumber = 50;

CGRect croprect = CGRectMake(croppingNumber, imageToCrop.size.height - croppingNumber, imageToCrop.size.width - croppingNumber, imageToCrop.size.height - croppingNumber);

// Draw new image in current graphics context
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], croprect);

// Create new cropped UIImage
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];

NSLog(@"cropped is: %@",croppedImage);

CGImageRelease(imageRef);

上述代码的结果:

cropped is: <UIImage:>, {1338, 50}

1 个答案:

答案 0 :(得分:0)

你需要留出足够的空间来覆盖正确的&amp;底部,所以将croppingNumber乘以2.否则,向右/向下移动50点只会将你的方块推到最右下角。

// Create rectangle from middle of current image
CGFloat croppingNumber = 50.0;
CGRect cropRect = CGRectMake(croppingNumber, croppingNumber, imageToCrop.size.width - (croppingNumber * 2), imageToCrop.size.height - (croppingNumber * 2));

// Draw new image in current graphics context
CGImageRef imageRef = CGImageCreateWithImageInRect(imageToCrop.CGImage, cropRect);

// Create new cropped UIImage
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];

NSLog(@"cropped is: %@",croppedImage);

CGImageRelease(imageRef);

对于将来看这个的人,我在下面的图片中显示了应用新rect后的坐标。这应该有助于可视化正在发生的事情。

enter image description here