【问题标题】:Trying to subclass UIImage but image is not being displayed in the image view尝试子类化 UIImage 但图像未显示在图像视图中
【发布时间】:2015-03-03 13:18:37
【问题描述】:

我正在尝试对 UIImage 进行子类化,这样我就可以为每个图像包含一个特殊的标题和我需要的其他信息。下面是我的 .m :

#import "Sticker.h"

@implementation Sticker



//custom init
-(instancetype)initWithTitle: (NSString *)title neededCount: (int)neededCount specialMesage: (NSString *)specialMesage andFilename: (NSString *)path {

    self = [super initWithContentsOfFile:path];

    if(self) {

        self.title=title;
        self.neededCount=neededCount;
        self.specialMessage=specialMessage;


    }

    return self;



}

我有一个带有单个 UIImageView 的空视图控制器,我正在尝试创建一个新的贴纸对象并使用 UIImageView 中的图像。代码:

Sticker *sticker = [[Sticker alloc] initWithTitle:@"crazy clown" neededCount:50 specialMessage:@"stackoverflow" andFilename:@"clown"];
    self.imageView.image=sticker;
    self.imageView.hidden=NO;

由于某种原因,图像没有显示,也没有错误消息。 “小丑”是 Images.xcassets 中的 pdf 图像。谁能给我一些指示为什么这不起作用?

【问题讨论】:

    标签: ios objective-c xcode uiimage


    【解决方案1】:

    你不应该子类化 UIImage 来尝试创建一个具有 UIImage 属性的类。

    @interface Sticker : NSObject
    @property (strong, nonatomic) NSString *title;
    @property (assign, nonatomic) int neededCount;
    @property (strong, nonatomic) NSString *specialMessage;
    @property (strong, nonatomic) UIImage *image;
    
    -(instancetype)initWithTitle:(NSString *)title neededCount:(int)neededCount specialMesage:(NSString *)specialMessage filename:(NSString *)name;
    
    @end
    
    @implementation Sticker
    
    -(instancetype)initWithTitle:(NSString *)title neededCount:(int)neededCount specialMesage:(NSString *)specialMessage filename:(NSString *)name {
        if(self = [super init]) {
            self.title = title;
            self.neededCount = neededCount;
            self.specialMessage = specialMessage;
            self.image = [UIImage imageNamed:name];
        }
        return self;
    }
    
    @end
    
    Sticker *sticker = [[Sticker alloc] initWithTitle:@"crazy clown" neededCount:50 specialMessage:@"stackoverflow" filename:@"clown"];
    self.imageView.image = sticker.image;
    

    无论如何initWithContentsOfFile 需要一个完整的路径,而不仅仅是文件名。

    【讨论】:

    • 为什么继承 UIImage 不好?
    • 这并不总是坏事,但在这种情况下,鉴于 UIImage 只是一个像素集合,问问自己,为什么 UIImage 应该知道 title、specialMessage 和 requiredCount?我没有看到在 UIImage 中添加这些信息的正当理由。
    【解决方案2】:

    子类化 UIImage 有点时髦。不过是可以的

    class PlaceHolderIcon: UIImage {
    
        override init() {
            let image = UIImage(named: "image-name")!
            super.init(cgImage: image.cgImage!, scale: UIScreen.main.scale, orientation: .up)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        required convenience init(imageLiteralResourceName name: String) {
            fatalError("init(imageLiteralResourceName:) has not been implemented")
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 1970-01-01
      • 2018-05-13
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 2020-03-23
      相关资源
      最近更新 更多