Sunday 15 July 2012

objective c - iOS: Align UIImageView to top -


I've got a visual controller, I'm using to load an image from the web. The image is usually going to be much more than the viewport, so I'm scaling it and putting it in UIImageView. The problem does not want to align at the top of the viewport. Here's the code I'll fix it:

  - (zero) viewDidoad {NSLD * imageURL = [NSD URL Youth Ringtones: [[Self-Exhibit] Image]]; NSDTA * ImageData = [NSDTATA DATAVITY content of the: imageURL]; UIImage * image = [UIImage imageWithData: imageData]; UIImageView * iv = [[UIImageView alloc] initWithImage: Image]; [Iv set content mode: UIViewContentModeScaleAspectFit]; [Iv set transclusionsautorerisingmask intactstationes: no]; [[See itself] Adsview: iv]; [Self-Setting View: iv]; NSDictionary * nameMap = @ {@ "imageView": [auto image view]}; NSArray * Horizontal Concept = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: | -10- [imageView] -10- |" Options: NSLayoutFormatAlignAllCenterX Metrics: Zero View: nameMap]; [[View Yourself] AddConstraints: Horizontal Consultants]; NSArray * verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @ "V: | -0- [imageView]" option: NSLayoutFormatAlignAllTop metric: zero view: nameMap]; [[View Yourself] AddConstraints: Vertical Consultants]; NSLog (@ "% @", NSStringFromCGRect ([[auto image view] frame))); }   

Finally the log shows that UIImageView is not the size of the original image size smaller version. I'm assuming that this is a problem, but I'm not sure how to fix it.

I did another solution: I modified this solution to work for me: / P>

The screenwid is less than 600px, so I only need to modify the image. I also know that the aspect ratio of the images I am using is already known.

This is my new view DeDeload

  - (zero) viewDidoad {NSDL * imageURL = [ns URL URLWithString: [[self-exhibition] image]]; NSDTA * ImageData = [NSDTATA DATAVITY content of the: imageURL]; UIImage * image = [UIImage imageWithData: imageData]; If ([[USScreen main screen] bound]. Size width = 600) {CGSize newSize = CGSizeMake (300.0, 140.6); UIGraphicsBeginImageContext (newSize); [Image DrawRight: CGRactack (0.0, 0.0, New Size. Width, New Size.)]; Image = UIGraphicsGetImageFromCurrentImageContext (); } NSLog (@ "% @", NSStringFromCGSize ([Image Size])); CGRTF frame = CGRactMake (0.0, 0.0, imageize.width, imaginesheight); UIImageView * iv = [[UIImageView alloc] initWithFrame: frame]; [Iv set image: image]; [Iv set content mode: UIViewContentModeScaleAspectFit]; [Iv set transclusionsautorerisingmask intactstationes: no]; [Iv setbackgram color: [UIColor redColor]]; [[See itself] Adsview: iv]; [Self-Setting View: iv]; [[Self image view] Setframe: CGRactam (0.0, 0.0, 300.0, 140.6)]; NSDictionary * nameMap = @ {@ "imageView": [auto image view]}; NSArray * Horizontal Concept = [NSLayoutConstraint constraintsWithVisualFormat: @ "H: | -10- [imageView] -10- |" Options: NSLayoutFormatAlignAllCenterX Metrics: Zero View: nameMap]; [[View Yourself] AddConstraints: Horizontal Consultants]; NSArray * verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat: @ "V: | -0- [imageView]" option: NSLayoutFormatAlignAllTop metric: zero view: nameMap]; [[View Yourself] AddConstraints: Vertical Consultants]; }    

No comments:

Post a Comment