JPG image doesn't load with UIImage imageNamed

前端 未结 2 1627
执笔经年
执笔经年 2020-12-16 12:07

I read on the reference that from iOS version 4.+ with the method imageNamed of UIImage object, the file extension is not required.

From

2条回答
  •  别那么骄傲
    2020-12-16 12:48

    If you have these images bundled in your app, you SHOULD know their extensions.

    If you're getting them from an online source and you have them as NSData, you can use this code here to determine the type.

    + (NSString *)contentTypeForImageData:(NSData *)data {
        uint8_t c;
        [data getBytes:&c length:1];
    
        switch (c) {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
        }
        return nil;
    }
    

    As per the top answer in this question.

提交回复
热议问题