CGImageCreateWithImageInRect没有在正确的位置裁剪

时间:2015-11-30 04:29:42

标签: ios crop

我的应用程序中有一个场景,我使用

截取视频的屏幕截图
[myMovieController requestThumbnailImagesAtTimes:@[@(myMovieController.currentPlaybackTime)] timeOption:MPMovieTimeOptionExact];

哪个工作正常。然后我必须在视频上裁剪触摸位置的图像。我添加了myMovieController的手势识别器。我从手势识别器获取触摸位置。

然后我使用以下代码进行屏幕截图

CGRect cropRect = tapCircleView.frame;
cropRect = CGRectMake(touchPoint.x * image.scale,
                      touchPoint.y * image.scale,
                      cropRect.size.width * image.scale,
                      cropRect.size.height * image.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect) ;
UIImage* cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
[self showImage:cropped];

其中cropRect的宽度和高度为150。

但是裁剪结果不是正确的x和y。并且结果图像非常像素化。 我尝试了所有解决方案,但它没有工作。 我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:0)

捕获屏幕截图的图像大小与运行应用程序的设备大小不同。 因此,使用以下代码更改图像大小,而不是使用相同的图像:

CGRect rect = CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,    [UIScreen mainScreen].bounds.size.height);
UIGraphicsBeginImageContext( rect.size );
[image drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *imageData = UIImagePNGRepresentation(picture1);
UIImage *img=[UIImage imageWithData:imageData];

现在只需在任何地方使用此图片!!

干杯。