建立分类UIImage+ToData
UIImage+ToData.h
#import <UIKit/UIKit.h>
@interface UIImage (ToData)
+(NSData*)getDataFromImage:(UIImage*)image;
@end
UIImage+ToData.m
#import "UIImage+ToData.h"
@implementation UIImage (ToData)
//将UIImage转换为NSData
+(NSData*)getDataFromImage:(UIImage*)image{
NSData *data;
/*判断图片是不是png格式的文件*/
if(UIImagePNGRepresentation(image))
data = UIImagePNGRepresentation(image);
/*判断图片是不是jpeg格式的文件*/
else
data = UIImageJPEGRepresentation(image,1.0);
return data;
}
@end
网友评论