James Cockerham
James Cockerham

Reputation: 409

Trying to do a simple scale of a picture in Objective-C

I'm new to the language, and I'm just trying to get a simple .jpg to show with the correct aspect ratio. Here's what I have now, and it's just showing in the whole frame filled. Is it because I'm drawing to the rect?

`UIImage *logoJames = [UIImage imageNamed:@"images.jpg"];
[logoJames drawInRect:rect];

Some comments suggesting classes.  I'm working through the Big Nerd Ranch book right now, and this is one of the challenge questions.

There is no reason to down vote some trying to learn.  That's bad community.

Here is the rest of my code.

//
//  BNRHypnosisView.m
//  Hypnosister
//
//  Created by James on 8/26/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

#import "BNRHypnosisView.h"

@implementation BNRHypnosisView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

    //setting all background colors of all bnrhypnosisviews to clear
    self.backgroundColor = [UIColor clearColor];
}
     return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect bounds = self.bounds;
CGPoint center;
center.x = bounds.origin.x + bounds.size.width / 2.0;
center.y = bounds.origin.y + bounds.size.height / 2.0;

UIImage *logoJAMES = [UIImage imageNamed:@"images.jpg"];

[logoJAMES drawInRect:rect];

// The circle will be the largest that will fit in the view
//float radius = MIN(bounds.size.width, bounds.size.height) / 2.0;

//the largest circle will circumscribe the view
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;

UIBezierPath *path = [[UIBezierPath alloc] init];

//define the path of the circle
//[path addArcWithCenter:center radius:radius startAngle:0.0 endAngle:M_PI * 2 clockwise:YES];

for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -=20) {

    [path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];

    [path addArcWithCenter:center radius:currentRadius startAngle:0.0 endAngle:M_PI * 2.0 clockwise:YES];
}

//change width of line
path.lineWidth = 10;

//change line color
[[UIColor lightGrayColor] setStroke];

    //Draw the line from above
[path stroke];




}


@end

Upvotes: 0

Views: 73

Answers (2)

Mike
Mike

Reputation: 9835

If all you want to do is have a UIImage maintain its aspect ratio, the easiest method is to create a UIImageView with the image and set the content mode to aspectFit.

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"images.jpg"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;

Then you can set the frame of the image view however you want and add it to your view however you want.

Upvotes: 0

nzeltzer
nzeltzer

Reputation: 521

If you're new to the language, you should consider using one of the Cocoa image view classes.

iOS: UIImageView

Mac: NSImageView.

Upvotes: 2

Related Questions