views:

142

answers:

2

I'm using CGRect to display an image. I'd like the CGRect to use the width and height of the image without me specifying it.

can this:

CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);

become this:

CGRectMake(0.0f, 40.0f, myImage.width, myImage.height);

some images get distorted when I specify the parameters.

here's the code:

CGRect myImageRect = CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:recipe.img]];

thanks for any help.

+2  A: 

This category on UIImage might be helpful.

Use it like this: aImage =[aImage imageByScalingProportionallyToSize: myImageRect]

@implementation UIImage (Extras)

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {

    UIImage *sourceImage = self;
    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;

    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor < heightFactor) 
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image

//        if (widthFactor < heightFactor) {
//          thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
//        } else if (widthFactor > heightFactor) {
//          thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
//        }

        //thumbnailPoint.x
    }


    // this is actually the interesting part:

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if(newImage == nil) NSLog(@"could not scale image");


    return newImage ;
}

@end;
vikingosegundo
+1  A: 

Once you have a UIImage, you can find it's size by looking at it's size property:

UIImage * image = [UIImage imageNamed:recipe.img];
CGRect rect = CGRectMake(0.0f, 40.0f, image.size.width, image.size.height);

UIImageView * imageView = [[UIImageView alloc] initWithFrame:rect];
[imageView setImage:image];
Douglas
that worked great. thanksif I wanted to center the rect would you do that w/ the '0.0f, 40.0f' points or is there a better way since the images are different sizes?
hanumanDev
You could default them to 0,0 then set the image view's center property: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/center Might be worthwhile skimming the documentation for UIImage, UIImageView and UIView because there are probably other things there which will be useful for you. Reading the list of available methods and properties at the top would be a good start, so would reading the introduction sections of those three pages, just to get an idea of what Apple puts where.
Douglas
ok, thanks again.
hanumanDev