[英]Saving thumbnail of UIImage flips the image sideways/upside down
我正在保存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;
我必須從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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.