繁体   English   中英

iPhone全屏调整大小的图像预览

[英]iphone full screen resized image preview

我有一个UIImageview,宽度为75,高度为75的视图。我从Camera或Photo Library捕获图像,调整其大小并设置为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; 
}

请帮我

将图像尺寸缩小到75x75后,图像质量会丢失-向上调整图像大小将无法恢复。 因此,您必须保留对原始未调整大小图像的引用,以将其传递给服务器。 您可以保留两个副本-手动调整大小的副本和要上载的原始副本,然后在完整尺寸的视图中简单地使用后者。

但是,正如robin在上面的注释中指出的那样,您也可以使用UIImageView的内容模式,使其根据内容模式自动调整视图内图像的大小。

与您的情况相关的内容模式是:

UIViewContentModeScaleToFill,UIViewContentModeScaleAspectFill,UIViewContentModeScaleAspectFit

这些都会导致在75x75 UIImageView中调整图像大小

myImageView.contentMode = UIViewContentModeScaleAspectFit;
myImageView.image = myUnscaledImage;

ScaleToFill将拉伸图像,以使其填充可用空间-即使这会使图像变形。

ScaleAspectFill将拉伸图像,以便最短的尺寸(宽度或高度)填充可用空间,并裁剪最长的尺寸(宽度或高度)。

ScaleAspectFit将拉伸图像,使最长尺寸仍适合可用空间,并在最短尺寸(水平或垂直)周围保留空白

UIViewContentModeScaleAspectFit是最有可能满足您目标的一种。

弹出全屏图像的一种方法是用透明按钮覆盖UIImageView,添加用于触摸的动作处理程序,并在其中提供一个模态视图控制器,该控制器具有将整个图像设置为视图的图像视图。 替代子类化图像视图并添加您自己的触摸处理,或使用手势识别器代替透明按钮。

如果全尺寸图像大于您的视图窗口,并且您希望它可以滚动,则将其放在滚动视图中。 如果您希望它也可缩放,则将其加载到Web视图内的img标签中,而不要使用滚动视图。

如果您希望显示是临时的,请使用NSTimer并在时间弹出时关闭模式视图控制器,或者您可以侦听上传http请求的结束,并在图像上传完成后关闭全尺寸视图。

您正在调整同一图像的大小,为什么不创建一个这样的尺寸减小的新图像

+ (UIImage*)scaledImageForImage:(UIImage*)image newSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM