我有一个宽度为75,高度为75的UIImageview视图。我从相机或图片库捕获图像,调整其大小并设置为imageview。
捕获的图像将上载到服务器,但会上载调整大小的图像,并且图像质量会丢失。从服务器检索时查看模糊图像。
现在,我需要的是,每当我点击imageview全屏图像预览应该显示和高质量的图像应该上传,而不是调整大小的图像
我使用以下代码调整了图像的大小
- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSize:(CGSize)newSize;
{
CGFloat targetWidth = newSize.width;
CGFloat targetHeight = newSize.height;
NSLog(@"target Width:%f",targetWidth);
NSLog(@"target Height:%f",targetHeight);
CGImageRef imageRef = [sourceImage CGImage];
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);
if (bitmapInfo == kCGImageAlphaNone) {
bitmapInfo = kCGImageAlphaPremultipliedLast;
}
CGContextRef bitmap;
if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
NSLog(@"ImageOrientation UP/DOWN");
} else {
bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);
}
if (sourceImage.imageOrientation == UIImageOrientationLeft) {
NSLog(@"ImageOrientation LEFT");
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -targetHeight);
} else if (sourceImage.imageOrientation == UIImageOrientationRight) {
NSLog(@"ImageOrientation RIGHT");
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -targetWidth, 0);
} else if (sourceImage.imageOrientation == UIImageOrientationUp) {
NSLog(@"ImageOrientation UP/DOWN");
// NOTHING
} else if (sourceImage.imageOrientation == UIImageOrientationDown) {
CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
CGContextRotateCTM (bitmap, radians(-180.));
}
// CGContextSetInterpolationQuality(bitmap, 1);
CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* newImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return newImage;
}请帮帮我
发布于 2011-09-13 12:35:53
一旦你将图像大小调整到75x75,质量就会丢失--向上调整大小不能恢复它。因此,您必须保留对原始未调整大小的图像的引用,以便上传到服务器。您可以保留两个副本-手动调整大小的副本和用于上传的原始副本,然后在全尺寸视图中简单地使用后者。
但是,正如robin在上面的评论中指出的那样,您可以使用UIImageView的内容模式让它根据内容模式自动调整视图中图像的大小。
适用于您的情况的相关内容模式包括:
UIViewContentModeScaleToFill、UIViewContentModeScaleAspectFill、UIViewContentModeScaleAspectFit
所有这些都将导致图像在75x75 UIImageView内调整大小
即
myImageView.contentMode = UIViewContentModeScaleAspectFit;
myImageView.image = myUnscaledImage;ScaleToFill将拉伸您的图像,使其填满可用空间-即使这会扭曲它。
ScaleAspectFill将拉伸您的图像,以便最短的尺寸(宽度或高度)填充可用空间,并裁剪最长的尺寸(宽度或高度)。
ScaleAspectFit将拉伸您的图像,使最长的维度仍然适合可用空间,并在最短维度(水平或垂直)周围留下空白空间
UIViewContentModeScaleAspectFit似乎最有可能满足您的目标。
弹出全屏图像的一种方法是用一个透明按钮覆盖你的UIImageView,为触摸添加一个操作处理程序,并在该视图中显示一个模式视图控制器,该控制器具有设置为视图的完整图像的图像视图。或者将图像视图子类化并添加您自己的触摸处理,或者使用手势识别器而不是透明按钮。
如果完整大小的图像比视图窗口大,并且您希望它是可滚动的,则将其放入滚动视图中。如果你希望它也是可缩放的,那么就把它加载到web视图中的img标签中,而不是使用滚动视图。
如果您希望显示是临时的,请使用NSTimer并在时间弹出时关闭模式视图控制器,或者您可以监听上传http请求的结尾,并在图像上传完成后关闭全尺寸视图。
发布于 2011-09-13 12:51:05
您正在调整相同图像的大小,为什么不创建一个尺寸减小的新图像,如下所示
+ (UIImage*)scaledImageForImage:(UIImage*)image newSize:(CGSize)newSize
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}https://stackoverflow.com/questions/7396701
复制相似问题