【问题标题】:iphone full screen resized image previewiphone全屏调整大小的图像预览
【发布时间】:2011-09-13 13:34:03
【问题描述】:

我有一个宽度为 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; 
}

请帮帮我

【问题讨论】:

  • 因此,您无需将其调整为 75x75 的小尺寸,您只需更改内容模式即可将其显示在一个小的 imageView 中。对于单个图像来说,这不是一个坏主意。
  • @robin: 如果我更新图像并留下内存警告,应用程序会在设备上崩溃

标签: iphone


【解决方案1】:

一旦您将图像大小调整为 75x75,质量就会丢失 - 重新向上调整大小不会恢复它。因此,您必须保留对原始未调整大小图像的引用才能通过上传到服务器。您可以这样做保留两个副本 - 您手动调整大小的副本和用于上传的原始副本,并在您的全尺寸视图中简单地使用后者。

但或者,正如 robin 在上面的评论中指出的那样,您可以使用 UIImageView 的内容模式让它根据内容模式自动调整视图中的图像大小。

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

UIViewContentModeScaleToFill、UIViewContentModeScaleAspectFill、UIViewContentModeScaleAspectFit

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

即

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

ScaleToFill 将拉伸您的图像以填充可用空间 - 即使这会扭曲它。

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

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

UIViewContentModeScaleAspectFit 似乎最符合您的目标。

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

如果完整大小的图像比您的视图窗口大,并且您希望它可以滚动,则将其放在滚动视图中。如果您希望它也可缩放,请将其加载到 web 视图内的 img 标签中,而不是使用滚动视图。

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

【讨论】:

  • 我保留了两个副本,即调整大小的图像和原始图像工作正常,但是当点击 imageview 时我如何实现全屏图像预览
  • 在我上面的回复中添加了一些建议
  • :感谢 gamozzii,它工作了,但一个问题是应用程序在全屏查看图像时崩溃并留下内存警告。我应该如何处理 1952x3264 像素的图像。
【解决方案2】:

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

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

    return newImage;
}

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 2015-02-10
    • 2011-01-06
    • 2012-03-10
    • 1970-01-01
    • 2012-09-25
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多