本文主要是介绍图片的处理-改尺寸、显圆形、判断图片类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//1.改变图像的尺寸,方便上传服务器
- (UIImage ) scaleFromImage: (UIImage ) image toSize: (CGSize) size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//2.保持原来的长宽比,生成一个缩略图
- (UIImage )thumbnailWithImageWithoutScale:(UIImage )image size:(CGSize)asize
{
UIImage *newimage;
if (nil == image) {
newimage = nil;
}
else{
CGSize oldsize = image.size;
CGRect rect;
if (asize.width/asize.height > oldsize.width/oldsize.height) {
rect.size.width = asize.height*oldsize.width/oldsize.height;
rect.size.height = asize.height;
rect.origin.x = (asize.width - rect.size.width)/2;
rect.origin.y = 0;
}
else{
rect.size.width = asize.width;
rect.size.height = asize.width*oldsize.height/oldsize.width;
rect.origin.x = 0;
rect.origin.y = (asize.height - rect.size.height)/2;
}
UIGraphicsBeginImageContext(asize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
3.显示圆形头像
[imgeView.layer setCornerRadius:CGRectGetHeight([self.img bounds]) / 2]; //修改半径,实现头像的圆形化
imgeView.layer.masksToBounds = YES;
4判断是否为PNG图片
png/jpg图片十六进制表示形式的前8位不同,可作为区分的标志;png图片前8位包含137 80 78 71 13 10 26 10(UIImagePNGRepresentation和UIImageJPEGRepresentation用来转成png或jpg格式 )
static unsigned char kPNGSignatureBytes[8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
static NSData *kPNGSignatureData = nil;
BOOL ImageDataHasPNGPreffix(NSData *data);
BOOL ImageDataHasPNGPreffix(NSData *data) {
NSUInteger pngSignatureLength = [kPNGSignatureData length];
if ([data length] >= pngSignatureLength) {
if ([[data subdataWithRange:NSMakeRange(0, pngSignatureLength)] isEqualToData:kPNGSignatureData]) {
return YES;
}
}
return NO;
}
kPNGSignatureData = [NSData dataWithBytes:kPNGSignatureBytes length:8];
这篇关于图片的处理-改尺寸、显圆形、判断图片类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!