以下代码获取当前屏幕的UIImage:
UIGraphicsBeginImageContext(self.view.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:ctx];
UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
如果我有一个 CGRect 矩形并且我只想获取该矩形中当前屏幕的 UIImage,我该怎么做?
最佳答案
获取矩形(裁剪)图像:
UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS You Need);
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];
使用以下返回 UIImage
的方法(您想要的图像大小)
- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
{
//CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
关于ios - 如何获取当前屏幕ios中特定区域的UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14477611/