解决iOS上传图片, 获取颠倒问题

2017-05-22  本文已影响218人  王誌傑

曾遇到这样的一个问题,  自己上传到服务器的图片, 在请求回来莫名奇妙的就会颠倒了

解决办法只需要, 在图片上传之前, 做一下处理就可以了

//图片处理,此方法解决了, (手机竖屏拍照,图片会横倒的问题)

- (UIImage*)fixOrientation:(UIImage*)aImage {

if(aImage.imageOrientation==UIImageOrientationUp)

returnaImage;

CGAffineTransformtransform =CGAffineTransformIdentity;

switch(aImage.imageOrientation) {

caseUIImageOrientationDown:

caseUIImageOrientationDownMirrored:

transform =CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);

transform =CGAffineTransformRotate(transform,M_PI);

break;

caseUIImageOrientationLeft:

caseUIImageOrientationLeftMirrored:

transform =CGAffineTransformTranslate(transform, aImage.size.width,0);

transform =CGAffineTransformRotate(transform,M_PI_2);

break;

caseUIImageOrientationRight:

caseUIImageOrientationRightMirrored:

transform =CGAffineTransformTranslate(transform,0, aImage.size.height);

transform =CGAffineTransformRotate(transform, -M_PI_2);

break;

default:

break;

}

switch(aImage.imageOrientation) {

caseUIImageOrientationUpMirrored:

caseUIImageOrientationDownMirrored:

transform =CGAffineTransformTranslate(transform, aImage.size.width,0);

transform =CGAffineTransformScale(transform, -1,1);

break;

caseUIImageOrientationLeftMirrored:

caseUIImageOrientationRightMirrored:

transform =CGAffineTransformTranslate(transform, aImage.size.height,0);

transform =CGAffineTransformScale(transform, -1,1);

break;

default:

break;

}

CGContextRefctx =CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,

CGImageGetBitsPerComponent(aImage.CGImage),0,

CGImageGetColorSpace(aImage.CGImage),

CGImageGetBitmapInfo(aImage.CGImage));

CGContextConcatCTM(ctx, transform);

switch(aImage.imageOrientation) {

caseUIImageOrientationLeft:

caseUIImageOrientationLeftMirrored:

caseUIImageOrientationRight:

caseUIImageOrientationRightMirrored:

// Grr...

CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);

break;

default:

CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);

break;

}

// And now we just create a new UIImage from the drawing context

CGImageRefcgimg =CGBitmapContextCreateImage(ctx);

UIImage*img = [UIImageimageWithCGImage:cgimg];

CGContextRelease(ctx);

CGImageRelease(cgimg);

returnimg;

}

上一篇下一篇

猜你喜欢

热点阅读