gpt4 book ai didi

iOS 7 :How to show images from photo album without strectch while placing it in uiimageview in larger size

转载 作者:行者123 更新时间:2023-11-29 10:39:10 25 4
gpt4 key购买 nike

我的任务是从相册中获取图像并将其显示在一个 ViewController 中,其中有一个全屏 UIImageView。这里面临的问题是,当涉及到 UIImageView

时,显示相机拍摄的小尺寸照片会被拉伸(stretch)

我已尝试使用所有纵横比模式来保持图像正常。但没有运气

提前致谢!

最佳答案

如果你想从相册中选择相同尺寸的图片那么

self.photoImgView.contentMode=UIViewContentModeScaleAspectFit;// is best

self.photoImgView.contentMode=UIViewContentModeScaleAspectFill; //if you want to fill the large imageview size with selected photo

self.photoImgView.contentMode=UIViewContentModeScaleToFill; // if you want to fill the large imageview size with selected photo

否则您可以尝试使用以下解决方案在分配给 imageview 之前调整图像大小。

 UIImage *uiImage=[info valueForKey:@"UIImagePickerControllerOrignalImage"];//UIImagePickerControllerEditedImage

CGSize size;
NSData *imageData;

imageData= UIImageJPEGRepresentation(uiImage, 1.0);


UIImage *galleryImage=[self squareImageWithImage:[UIImage imageWithData:imageData] scaledToSize:CGSizeMake(320, 320)];

photoImgView.image=galleryImage;

-(UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
double ratio;
double delta;
CGPoint offset;

//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);

//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.width / image.size.width;
delta = (ratio*image.size.width - ratio*image.size.height);
offset = CGPointMake(delta/2, 0);
} else {
ratio = newSize.width / image.size.height;
delta = (ratio*image.size.height - ratio*image.size.width);
offset = CGPointMake(0, delta/2);
}

//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x, -offset.y,
(ratio * image.size.width) + delta,
(ratio * image.size.height) + delta);


//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
} else {
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

希望对你有帮助

关于iOS 7 :How to show images from photo album without strectch while placing it in uiimageview in larger size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25376215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com