Bronz Challenge | Appropriate for every screen/image size


#1

Hi there,

I just put here my solution for putting image. this code put any sized image (no matter landscape or portrait) on center of any display size (portrait/landscape) with condition that image shouldn’t fill more than 2/3 of whole screen.

BNRHypnisysView.m

..
..
    //create frame for image with this circument
    // - should position on center of screen
    // - should not violate 2/3 of whole frame bounds
    // - should keep scale of image and don't shrink/strech it
    

    //calculate rect of 2/3 sized of bounds
    CGSize permitSize = CGSizeMake((bounds.size.width / 3 * 2), bounds.size.height / 3 * 2);
    
    
    //Load Image & find image size
    UIImage *logoImage = [UIImage imageNamed:@"logo"];
    CGSize imageSize = logoImage.size;

    
    //Make image final CGSize
    CGSize finalSize;
    finalSize.width = permitSize.width;
    finalSize.height = (permitSize.width * imageSize.height) / imageSize.width;
    //check whether image height violates permited height
    if (finalSize.height > permitSize.height){
        //we should set image height to maximum permit height
        finalSize.height = permitSize.height;
        finalSize.width = (permitSize.height * imageSize.width) / imageSize.height;
    }
    CGPoint startPoint = CGPointMake((bounds.size.width - finalSize.width) / 2,(bounds.size.height - finalSize.height) / 2);
    CGRect imageRect = {startPoint,finalSize};
    [logoImage drawInRect:imageRect];
}