user4191537 user4191537 - 8 months ago 51
iOS Question

Init image view with frame and image

I currently have the code below to load in a user's profile picture in my iOS app. However, this is a rewrite by me to load it from an url.

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];
NSURL *url = [NSURL URLWithString:@""];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 50.0;
imageView.layer.borderColor = [UIColor whiteColor].CGColor;
imageView.layer.borderWidth = 3.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;

The old code had this
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)];
so that the image wouldn't be too large. How can I incorporate that into my code?

And changing
is not an option, since that is just a placeholder.


Use UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)]; ?

You can set the image later using


Also, you should probably take out

imageView.layer.rasterizationScale and imageView.layer.shouldRasterize,

you probably don't need them.

on a side note

( +[NSData dataWithContentsOfURL:] should NEVER be used in such case...)