Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios imageview图像填充拟合模式_Ios_Objective C - Fatal编程技术网

Ios imageview图像填充拟合模式

Ios imageview图像填充拟合模式,ios,objective-c,Ios,Objective C,我有一个表视图单元格[prototype],其中添加了一个imageview。现在,我在size inspector中手动设置此imageview的高度和宽度。但是我用的是 [cell viewWithTag:12345] 在我的objective c代码中获取视图指针,并更新图像视图的图像。但是,我发现我的手动约束被覆盖,并且图像具有自己的大小。我在属性检查器和编程中尝试了各种模式(方面填充、方面匹配等) 所以我的问题是如何设置约束?我错在哪里?为什么它们不起作用?使用以下方法对图像的特定高

我有一个表视图单元格[prototype],其中添加了一个imageview。现在,我在size inspector中手动设置此imageview的高度和宽度。但是我用的是

[cell viewWithTag:12345]
在我的objective c代码中获取视图指针,并更新图像视图的图像。但是,我发现我的手动约束被覆盖,并且图像具有自己的大小。我在属性检查器和编程中尝试了各种模式(方面填充、方面匹配等)


所以我的问题是如何设置约束?我错在哪里?为什么它们不起作用?

使用以下方法对图像的特定

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

此方法返回新图像,并带有所需的特定大小

对特定的图像使用以下方法

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

此方法返回新图像,并带有所需的特定大小

首先,我要检查问题是在于图像视图的内容模式还是图像视图的大小。您的imageview也可能因为自动调整大小掩码而调整大小。如果使用初始化图像视图

- (id)initWithImage:(UIImage *)image

方法您的imageview将自动调整为图像大小。

首先,我将检查问题是在于图像视图的内容模式还是图像视图的大小。您的imageview也可能因为自动调整大小掩码而调整大小。如果使用初始化图像视图

- (id)initWithImage:(UIImage *)image

方法您的imageview将自动调整为图像大小。

以下代码非常有用

案例1:

   -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...

    return cell;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...


    return cell;
}
输出:

和案例2:

   -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...

    return cell;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...


    return cell;
}
输出:

实际图像大小为460x246,但您可以根据帧找到差异


谢谢

以下代码非常有用

案例1:

   -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...

    return cell;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...


    return cell;
}
输出:

和案例2:

   -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...

    return cell;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...


    return cell;
}
输出:

实际图像大小为460x246,但您可以根据帧找到差异


谢谢

您询问了一个关于UIImageView约束的问题,并检查了关于调整UIImage大小的答案?也许你应该问清楚你的问题…抱歉。它解决了我现在,所以我只是点击它作为答案。如果我找到任何解决问题的其他答案,我也会给出这个答案。我的唯一意图是,如果有人登陆此页面,他们将知道哪些都适合我。您已经询问了有关UIImageView约束的问题,并检查了有关调整UIImage大小的答案?也许你应该问清楚你的问题…抱歉。它解决了我现在,所以我只是点击它作为答案。如果我找到任何解决问题的其他答案,我也会给出这个答案。我唯一的意图是,如果有人登陆此页面,他们将知道哪些对我有效。我的初始化如下:UIImageView imageThumbnail=(UIImageView)[带标签的单元格视图:12345];它不是init方法。您只获得了UIImageView实例,您已经有了它。如何创建它们?如何设置图像?imageThumbnail.image(imageThumbnail是imageView*)=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbSmallURL]]哦,我明白你的意思。那么应该如何继续停止覆盖呢?你不改变imageview大小的方式。我假设你自己设置了帧,你不想改变大小?你可以设置断点(例如在didSelectRowAtIndexPath中)并检查imageview的框架和内容模式。是否在横向视图中使用tableview?我的初始化如下:UIImageView imageThumbnail=(UIImageView)[带标记的单元格视图:12345];它不是init方法。您只获取UIImageView实例,而您已经拥有了UIImageView实例。如何创建它们?如何设置图像?imageThumbnail.image(imageThumbnail是imageView*)=[UIImageImageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbSmallURL]]哦,我明白你的意思了。那么应该如何继续停止覆盖呢?你不改变imageview大小的方式。我假设你自己设置了框架,但你不想改变大小?你可以设置一个断点(例如在didSelectRowAtIndexPath中)并检查你的imageview框架和内容模式。你在横向中使用tableview吗?