Сохранение миниатюры UIImage переворачивает изображение в сторону / вверх ногами
Я сохраняю эскиз UIImage
чтобы улучшить производительность моего UICollectionView
прокрутки. Все изображения сохранялись в вертикальном положении, пока я не попытался сохранить панорамный снимок, снятый снизу вверх (например: высота - 3000, ширина - 1000). Это спасло с ног на голову. Я делаю портретную съемку и поворачиваю на 90, потому что изначально изображение вбок. Есть ли способ установить каждый тип изображения в вертикальном положении с помощью строки кода, или мне нужно поймать каждый тип фотографии и настроить его соответствующим образом? И если так, как?
- (void) createThumbnail: (UIImage *) image{
CGSize size = image.size;
CGFloat imageHeight = size.height;
CGFloat imageWidth = size.width;
CGSize croppedSize;
CGFloat ratio = 200.0;
CGFloat offsetX = 0.0;
CGFloat offsetY = 0.0;
if (imageWidth > imageHeight) {
offsetX = (imageHeight - imageWidth) / 2;
croppedSize = CGSizeMake(imageHeight, imageHeight);
} else {
offsetY = (imageWidth - imageHeight) / 2;
croppedSize = CGSizeMake(imageWidth, imageWidth);
}
CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], clippedRect);
CGRect rect = CGRectMake(0.0, 0.0, ratio, ratio);
UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail;
if ((imageWidth > imageHeight) || (imageWidth == imageHeight)) {
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationUp];
}else{
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationRight];
}
UIGraphicsEndImageContext();
return thumbnail;
2 ответа
Я должен был получить ориентацию изображения из выбранного изображения в didFinishPickingMediaWithInfo
вместо обрезанного эскиза.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
choosenImage = info[UIImagePickerControllerOriginalImage];
long orientation = choosenImage.imageOrientation;
UIImage *thumbnail = [self createThumbnail:choosenImage withOrientation: orientation];
}
UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail;
if (orientation == UIImageOrientationUp) {
thumbnail = UIGraphicsGetImageFromCurrentImageContext();
}else if (orientation == UIImageOrientationRight){
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationRight];
}else if (orientation == UIImageOrientationLeft){
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationLeft];
}else if (orientation == UIImageOrientationDown){
thumbnail = [[UIImage alloc]initWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:1.0 orientation:UIImageOrientationUpMirrored];
}
UIGraphicsEndImageContext();
Решение для изображений из приложения для фотографий: https://developer.apple.com/library/ios/documentation/AssetsLibrary/Reference/ALAssetRepresentation_Class/
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:
[defaultRepresentation fullScreenImage]
scale:[defaultRepresentation scale]
orientation:0];