【问题标题】:Resize individual images in array of UIImageViews调整 UIImageViews 数组中单个图像的大小
【发布时间】:2011-03-22 13:14:37
【问题描述】:

在我的 iPhone 应用程序中,我需要调整数组中的一些 UIImageView 类型的图像的大小。我没有为此任务使用 Interface Builder。

我收到此错误:

左运算符赋值需要左值

使用以下代码:

CGSize newsize;

for (int i = 0; i<5; i++) 
{
    // ball[] is of type UIImageView *
    ball[i].center = CGPointMake(ball[i].center.x-5,ball[i].center.y);

    if (ball[i].center.x <= 36) 
    {
        while(ball[i].frame.size.width>0 && ball[i].frame.size.height>0) 
        {
            newsize.height =val2--;
        newsize.width =val2--;      
        ball[i].frame.size = newsize; // This line gives the error

        }

    }   
}

如果这不是正确的方法,您能告诉我应该如何调整数组中的图像大小吗?

【问题讨论】:

    标签: objective-c cocoa-touch ios uiimageview image-resizing


    【解决方案1】:

    你的意思是,你想裁剪图像?

    要调整图像大小,请尝试以下代码:

    -(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {
    
        CGImageRef imageRef = [image 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;  
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-30
      • 2013-10-24
      • 2021-04-21
      • 2018-11-15
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多