나는 here에 제공된 UIImage+Resize
카테고리를 사용했다. 나는이 일을하기 위해 몇 가지 새로운 방법을 사용했다. 더 잘 보이고 테스트되지 않는 코드를 다시 포맷했지만 작동해야합니다. :)
- (UIImage*)cropAndResizeAspectFillWithSize:(CGSize)targetSize
interpolationQuality:(CGInterpolationQuality)quality {
UIImage *outputImage = nil;
UIImage *imageForProcessing = self;
// crop center square (AspectFill)
if (self.size.width != self.size.height) {
CGFloat shorterLength = 0;
CGPoint origin = CGPointZero;
if (self.size.width > self.size.height) {
origin.x = (self.size.width - self.size.height)/2;
shorterLength = self.size.height;
}
else {
origin.y = (self.size.height - self.size.width)/2;
shorterLength = self.size.width;
}
imageForProcessing = [imageForProcessing normalizedImage];
imageForProcessing = [imageForProcessing croppedImage:CGRectMake(origin.x, origin.y, shorterLength, shorterLength)];
}
outputImage = [imageForProcessing resizedImage:targetSize interpolationQuality:quality];
return outputImage;
}
// fix image orientation, which may wrongly rotate the output.
- (UIImage *)normalizedImage {
if (self.imageOrientation == UIImageOrientationUp) return self;
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
[self drawInRect:(CGRect){0, 0, self.size}];
UIImage *normalizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return normalizedImage;
}
캡쳐 된 이미지는 가로 채우기 용으로 크기를 조정하기 때문에 미리보기 이미지와 다릅니다. 맞습니까? 그래서 대신 속성을 'AVLayerVideoGravityResizeAspect'로 변경하거나 미리보기 레이어의 크기를 캡처 이미지의 비율과 일치하도록 조정하지 않으시겠습니까? – MiguelB
문제점의 해결책을 찾으셨습니까? – Dejell