I found the below code in Finding image type from NSData or UIImage which helps to check four different image types of a UIimage
(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;
}
I want to know how to find the file is a bitmap image or not that it has .bmp extension. can someone please help me with it. Either modify the above code to find bmp as well or please provide me a solution with some code.
thanks in adnvance
You can, change image to NSData
by usingUIImageJPEGRepresentation(<#UIImage *image#>, <#CGFloat compressionQuality#>)
ORUIImagePNGRepresentation(<#UIImage *image#>)
method. Call it in this way:
- (void) yourMethod{
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSString *str = [self contentTypeForImageData:imageData];
}
- (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:
break;
case 0x42:
return @"image/bmp";
case 0x4D:
return @"image/tiff";
}
return nil;
}
According to wikipedia a non-OS/2 BMP file start's with 0x42 0x4D
(BM in ascii).
case 0x42:
return @"image/bmp";
来源:https://stackoverflow.com/questions/17828044/is-it-possible-to-find-the-file-extension-of-a-uiimage