【问题标题】:How to set UIImageView Size and how to make sure it stays that size all the time?如何设置 UIImageView 大小以及如何确保它始终保持该大小?
【发布时间】:2016-04-05 10:18:29
【问题描述】:

我一直在使用这个库在 iOS 上试验增强现实:https://github.com/promet/PRAugmentedReality

我添加了另一个名为“POIDetails”的 xib,用于显示我的兴趣点的详细信息。我通过这段代码添加了一个返回相机屏幕的按钮:

[self dismissViewControllerAnimated:self completion:nil];

但是当我返回 AR 屏幕时,POI 图像变得巨大。

我尝试将高度约束设置为 50,将纵横比设置为 1:1,但这并没有起到任何作用。我尝试像这样使用 sizeThatFits:

[myPOI sizeThatFits:CGSizeMake(50, 50)];

但它也没有用。在看到没有区别后,我将我在图像大小上所做的一切都放回去了。

您可以在下面看到之前和之后的图片以及用于下载我的项目的 google drive 链接。我怎样才能确保这张图片不会超出 50 像素的高度?

链接:https://drive.google.com/file/d/0B-cDfWHidgvcVVFRNll0OVR2UTA/view?usp=sharing

这是我的 ARObject.m 文件

#import "ARObject.h"
#import "POIDetails.h"


@interface ARObject ()

@end


@implementation ARObject

@synthesize arTitle, distance;

- (id)initWithId:(int)newId
           title:(NSString*)newTitle
     coordinates:(CLLocationCoordinate2D)newCoordinates
andCurrentLocation:(CLLocationCoordinate2D)currLoc
{
    self = [super init];
    if (self) {
        arId = newId;

        arTitle = [[NSString alloc] initWithString:newTitle];

        lat = newCoordinates.latitude;
        lon = newCoordinates.longitude;

        distance = @([self calculateDistanceFrom:currLoc]);

        [self.view setTag:newId];
    }
    return self;
}

-(double)calculateDistanceFrom:(CLLocationCoordinate2D)user_loc_coord
{
    CLLocationCoordinate2D object_loc_coord = CLLocationCoordinate2DMake(lat, lon);

    CLLocation *object_location = [[CLLocation alloc] initWithLatitude:object_loc_coord.latitude
                                                              longitude:object_loc_coord.longitude];
    CLLocation *user_location = [[CLLocation alloc] initWithLatitude:user_loc_coord.latitude
                                                            longitude:user_loc_coord.longitude];

    return [object_location distanceFromLocation:user_location];
}
-(NSString*)getDistanceLabelText
{
    if (distance.doubleValue > POINT_ONE_MILE_METERS)
         return [NSString stringWithFormat:@"%.2f mi", distance.doubleValue*METERS_TO_MILES];
    else return [NSString stringWithFormat:@"%.0f ft", distance.doubleValue*METERS_TO_FEET];
}

- (NSDictionary*)getARObjectData
{
    NSArray *keys = @[@"id",@"title", @"latitude", @"longitude", @"distance"];

    NSArray *values = @[@(arId),
                       arTitle,
                       @(lat),
                       @(lon),
                       distance];
    return [NSDictionary dictionaryWithObjects:values forKeys:keys];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [titleL setText:arTitle];

    [distanceL setText:[self getDistanceLabelText]];
}

-(void)myPOITapping:(UIGestureRecognizer *)recognizer
{
    NSLog(@"image click");
    POIDetails *poiDetails = [[POIDetails alloc] initWithNibName:@"POIDetails" bundle:nil]; // constructor
    [self presentViewController:poiDetails animated:YES completion:nil];
}

// viewDidLoad wasn't in the library, timur added this method
- (void)viewDidLoad {
    myPOI.contentMode = UIViewContentModeScaleToFill;
    myPOI.translatesAutoresizingMaskIntoConstraints=YES;

    [myPOI setUserInteractionEnabled:YES];
    UITapGestureRecognizer *myPOITap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myPOITapping:)];
    [myPOITap setNumberOfTapsRequired:1];
    [myPOI addGestureRecognizer:myPOITap];
    [self.view addSubview:myPOI];
}

#pragma mark -- OO Methods

- (NSString *)description {
    return [NSString stringWithFormat:@"ARObject %d - %@ - lat: %f - lon: %f - distance: %@",
            arId, arTitle, lat, lon, distance];
}

@end

编辑:

我在我的 ARObject.m 文件的 viewDidLoad 函数中添加了以下代码,它似乎将图像保持在设定的分辨率。

myPOI.contentMode = UIViewContentModeScaleToFill; myPOI.translatesAutoresizingMaskIntoConstraints=YES;

但我的距离标签仍然存在问题。我将顶部和底部间距约束设置为最近的邻居,标签现在是一半,但仍与我的兴趣点图像分开,如下所示:




之前:

之后:

【问题讨论】:

    标签: ios objective-c swift uiimageview augmented-reality


    【解决方案1】:

    我尝试将高度约束设置为 50,将纵横比设置为 1:1,但这没有任何好处

    当您使用约束时,检查您的UIImageView 实例的translatesAutoresizingMaskIntoConstraints 属性是否设置为NO

    同时检查UIImageView的实例contentMode属性是否设置为UIViewContentModeScaleToFill


    ARObject.m

    CGRect myRect;
    
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        /*
         keep existed in this method code
        */
    
        /*
         move [self.view addSubview:myPOI] line to here, leave all other in viewDidLoad 
        */
    
        if (myRect.size.height != 0) {self.view.frame = myRect;}
        [self.view addSubview:myPOI];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    
        myRect = self.view.frame;
        [myPOI removeFromSuperview];
    }
    
    - (void)viewDidLoad
    {
        myRect = CGRectZero;
    
        /*
         all your other code
        */    
    }
    

    很遗憾,我无法测试代码,因为此项目需要设备。

    【讨论】:

    • 我在 myARObject.m 文件的 viewDidLoad 中添加了以下代码。它似乎解决了图像调整大小的问题,但距离标签仍然高于这一点。在一分钟内将屏幕截图添加到我的问题中。 myPOI.contentMode = UIViewContentModeScaleToFill; myPOI.translatesAutoresizingMaskIntoConstraints=YES;
    • 对不起,如果我不够清楚。如果您使用约束,则需要设置 myPOI.translatesAutoresizingMaskIntoConstraints=NO
    • 我添加了 ARObject.m 文件。当我将 translatesAutoresizingMaskIntoConstraints 设置为 NO 时,图像也无法正确显示。当我将其设置为 YES 时它可以正常工作
    • 你可以从上面的谷歌驱动链接下载项目
    • 谢谢。在 superview 中定位 ARObject 存在问题。无需代码分析的快速简单解决方案是将您的代码从 viewDidLoad 方法移动到 viewWillAppear,并添加您应该“回滚”的方法 viewWillDissapear,从 superview 中删除 myPOI。
    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2023-03-23
    相关资源
    最近更新 更多