【问题标题】:Init image view with frame and image使用框架和图像初始化图像视图
【发布时间】:2014-11-19 20:03:13
【问题描述】:

我目前有以下代码可以在我的 iOS 应用程序中加载用户的个人资料图片。但是,这是我重写的,以便从 url 加载它。

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 184.0f)];
NSURL *url = [NSURL URLWithString:@"http://placehold.it/250x250"];
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;

旧代码有这个UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, 100, 100)];,这样图像就不会太大。如何将其合并到我的代码中?

并且将http://placehold.it/250x250 更改为http://placehold.it/100x100 不是一种选择,因为这只是一个占位符。

【问题讨论】:

    标签: ios objective-c xcode uiimageview


    【解决方案1】:

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

    您可以稍后使用

    设置图像
    imageView.image
    

    另外,你应该拿出来

    imageView.layer.rasterizationScaleimageView.layer.shouldRasterize

    你可能不需要它们。

    附注

    +[NSData dataWithContentsOfURL:] 应该绝不在这种情况下使用...)

    【讨论】:

    • 那你将如何重写它?我不太明白你说的You can set the image later using imageView.image 是什么意思。
    • 代替UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 行,您可以改写UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,40,100,100)]; 并继续其余的imageView 自定义。要“稍后设置图像”,您只需使用 imageView.image = image;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多