调整 UIImageView 数组中单个图像的大小

发布于 2024-10-25 09:31:19 字数 713 浏览 0 评论 0原文

在我的 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

        }

    }   
}

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

In my iPhone app, I need to resize some images of type UIImageView which I have in an array. I didn't use Interface Builder for this task.

I'm getting this error:

Lvalue required as left operator assgnment

using the following code:

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

        }

    }   
}

Can you tell me how should I resize my images in the array, if this is not the correct method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2024-11-01 09:31:19

您的意思是,您想要裁剪图像?

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

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

You mean, you want to crop the images?

To resize the images, try this code:

-(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;  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文