简体   繁体   English

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

[英]iphone full screen resized image preview

I have a view with UIImageview of width 75 and height 75. I capture an image from Camera or Photo Library,resize it and set to imageview. 我有一个UIImageview,宽度为75,高度为75的视图。我从Camera或Photo Library捕获图像,调整其大小并设置为imageview。

The captured image is uploaded to server but resized image is uploaded and quality of image is lost. 捕获的图像被上载到服务器,但是调整大小的图像被上载,并且图像质量下降。 A blur image is viewed while retrieving from server. 从服务器检索时查看模糊图像。

Now, what i need is, whenever i tap on imageview a full screen image preview should display and image with high quality should be uploaded instead of resized image 现在,我需要的是,每当我点击imageview时,都应该显示全屏图像预览,并且应该上传高质量的图像,而不是调整大小的图像

I resized image using the following code 我使用以下代码调整了图片的大小

- (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; 
}

Please help me 请帮我

Once you have resized the image down to 75x75 the quality is lost - resizing it back upwards won't restore it. 将图像尺寸缩小到75x75后,图像质量会丢失-向上调整图像大小将无法恢复。 So you have to keep a reference to the original unresized image to pass for uploading to the server. 因此,您必须保留对原始未调整大小图像的引用,以将其传递给服务器。 You could do this keeping two copies - your manually resized copy and the original copy for upload, and simply using the latter in your full sized view. 您可以保留两个副本-手动调整大小的副本和要上载的原始副本,然后在完整尺寸的视图中简单地使用后者。

But alternately, as robin has noted in the comment above, you can use the content mode of your UIImageView to have it automatically resize the image within the view based on the content mode. 但是,正如robin在上面的注释中指出的那样,您也可以使用UIImageView的内容模式,使其根据内容模式自动调整视图内图像的大小。

The relevant content modes for your situation are: 与您的情况相关的内容模式是:

UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFill, UIViewContentModeScaleAspectFit UIViewContentModeScaleToFill,UIViewContentModeScaleAspectFill,UIViewContentModeScaleAspectFit

These will all cause the image to be resized within the 75x75 UIImageView 这些都会导致在75x75 UIImageView中调整图像大小

ie

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

The ScaleToFill will stretch your image so that it fills the available space - even if this distorts it. ScaleToFill将拉伸图像,以使其填充可用空间-即使这会使图像变形。

The ScaleAspectFill will stretch your image so that the shortest dimension (width or height) fills the available space, and will crop the longest dimension (width or height). ScaleAspectFill将拉伸图像,以便最短的尺寸(宽度或高度)填充可用空间,并裁剪最长的尺寸(宽度或高度)。

The ScaleAspectFit will stretch your image so that the longest dimension still fits into the available space, and leave empty space around the shortest dimension (horizontal or vertical) ScaleAspectFit将拉伸图像,使最长尺寸仍适合可用空间,并在最短尺寸(水平或垂直)周围保留空白

UIViewContentModeScaleAspectFit is the one that seems most likely to fit your goal. UIViewContentModeScaleAspectFit是最有可能满足您目标的一种。

One way to pop up a full screen image would be to cover your UIImageView with a transparent button, add an action handler for touch, and in that present a modal view controller which has an image view for your full image set to the view. 弹出全屏图像的一种方法是用透明按钮覆盖UIImageView,添加用于触摸的动作处理程序,并在其中提供一个模态视图控制器,该控制器具有将整个图像设置为视图的图像视图。 Alternately subclass image view and add your own touch handling or use a gesture recognizer instead of a transparent button. 替代子类化图像视图并添加您自己的触摸处理,或使用手势识别器代替透明按钮。

If the full size image is larger than your view window and you want it to be scrollable then put it inside a scrollview. 如果全尺寸图像大于您的视图窗口,并且您希望它可以滚动,则将其放在滚动视图中。 If you want it to be zoomable as well then load it into img tags inside a web view instead of using a scroll view. 如果您希望它也可缩放,则将其加载到Web视图内的img标签中,而不要使用滚动视图。

If you want the display to be temporary use an NSTimer and close the modal view controller when the time pops, or you could listen for the end of your upload http request and close the full size view when the image has finished uploading. 如果您希望显示是临时的,请使用NSTimer并在时间弹出时关闭模式视图控制器,或者您可以侦听上传http请求的结束,并在图像上传完成后关闭全尺寸视图。

You are resizing the same image, why don't you create a new image of reduced size like this 您正在调整同一图像的大小,为什么不创建一个这样的尺寸减小的新图像

+ (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