【问题标题】:UIImage resizing. OK on simulator, not on deviceUIImage 调整大小。在模拟器上可以,在设备上不行
【发布时间】:2012-05-25 18:18:01
【问题描述】:

在一个 iPhone 应用程序中,我编写了一些代码来调整从相册中获取的图像的大小,以便将其用作应用程序的背景。 该代码的灵感来自我在网上找到的内容。即这里: http://forrst.com/posts/UIImage_simple_resize_and_crop_image-sUG#comment-land 它在模拟器上运行良好。但看起来设备上根本没有调整大小。这是为什么?有什么想法吗?

感谢您的任何提示。

【问题讨论】:

    标签: ios4 uiimage ios-simulator device


    【解决方案1】:

    嗨,米歇尔。

    我不了解您的代码,但我正在使用此代码,您可以尝试 这你会找到你的解决方案。

    在这里您需要传递图像以及所需的宽度和高度 这个函数中的那个图像,你会得到另一个图像 新尺寸。

     -(UIImage *)resizeImage:(UIImage *)image3 width:(int)width height:(int)height {
    
            CGImageRef imageRef = [image3 CGImage];
            CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
    
            //if (alphaInfo == kCGImageAlphaNone)
            alphaInfo = kCGImageAlphaNoneSkipLast;
    
            CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
            CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
            CGImageRef ref = CGBitmapContextCreateImage(bitmap);
            UIImage *result = [UIImage imageWithCGImage:ref];
    
            CGContextRelease(bitmap);
            CGImageRelease(ref);
    
            return result;  
    
        }
    

    【讨论】:

    • 谢谢卡提克;我试过你的代码。这是结果。它适用于设备。在模拟器上似乎不稳定。更准确地说,如果我在纵向模式下使用该功能,那么它将无法在横向模式下工作,反之亦然。我还没有深入研究这一点,以确切地知道发生了什么。我喜欢你的解决方案的简短性。但我需要澄清我提到的问题,以完成我的代码。
    • 我的第一条评论可能不准确,而不是方向,似乎代码在某些图片上运行良好,而在其他一些图片上运行不畅。我还不知道为什么。
    • 我的意思是,当我使用你的代码时;这张照片效果很好:public-domain-photos.com/animals/… 但不适用于这些照片:pdclipart.org/displayimage.php?album=19&pos=27 pdclipart.org/displayimage.php?album=21&pos=8 我在模拟器和设备上都试过了;我得到相同的结果。但是,但是当我在模拟器和设备上使用之前使用的代码的末尾添加您的代码时。再次感谢您的提示。
    猜你喜欢
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 2018-01-04
    • 1970-01-01
    • 2016-02-21
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多