如何设置UIImageView大小以及如何确保它始终保持这个大小?

时间:2016-04-05 10:18:30

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

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

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

[self dismissViewControllerAnimated:self completion:nil];

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

我试图将高度约束设置为50,并将方面设置为1:1但它没有任何好处。我试着像这样使用sizeThatFits:

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

但它也没有用。在看到没有区别之后,我放弃了我在图像尺寸上所做的一切。

下面你可以看到前后图像和谷歌驱动器链接下载我的项目。如何确保此图像不会超出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;

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

enter image description here

在:  enter image description here

在: enter image description here

1 个答案:

答案 0 :(得分:1)

  

我试图将高度约束设置为50,并将方面设置为1:1但它没有做任何好事

使用约束时,检查translatesAutoresizingMaskIntoConstraints实例的UIImageView属性是否设置为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
    */    
}

不幸的是我无法测试代码,因为这个项目需要设备。