UIImage 方向
UIImage imageOrientation是相对当前屏幕的横竖屏来判断方向
如果本身是横屏, 照片也是横屏的话, 方向是正方向
BOOL b1 = (originalImage.imageOrientation == UIImageOrientationUp || originalImage.imageOrientation == UIImageOrientationDown);
BOOL b2 = (originalImage.imageOrientation == UIImageOrientationLeft || originalImage.imageOrientation == UIImageOrientationRight);
BOOL b3 = originalImage.imageOrientation == UIImageOrientationLeft;
BOOL b4 = originalImage.imageOrientation == UIImageOrientationRight;
横屏中, 正面照是:UIImageOrientationRight
upsidedown 照片是 UIImageOrientationLeft
//根据给定得图片,从其指定区域截取一张新得图片
-(UIImage *)getImageFromImage:(UIImage *)image rect:(CGRect)rect
{
//开始上下文
UIGraphicsBeginImageContext(rect.size);
CGImageRef imageRef = image.CGImage;
//创建子图像索引
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, rect);
//获取当前上下文
CGContextRef context = UIGraphicsGetCurrentContext(); //在当前上下文下, 定位为rect下, 画出子图像索引
CGContextDrawImage(context,CGRectMake(, , rect.size.width, rect.size.height), subImageRef); //创建图片对象, 根据图像索引
UIImage *subImage = [[UIImage alloc] initWithCGImage:subImageRef]; //销毁创建的子图像索引,不然会导致内存泄露
CGImageRelease(subImageRef);
//结束上下文
UIGraphicsEndImageContext();
return subImage;
}
被render成为新图片之后 uiimage默认的imageOrientation是正方向(UIImageOrientationUp)
图片方向转换
这几天写个拍照,或者从相册中选择照片,进行剪切,然后分享.结果出现了,剪切后图片颠倒或者旋转90度的问题. 找了很久才发现是忽略imageOrientation这个属性. 以下为解决方法: [cpp] view plaincopy
+ (UIImage *)fixOrientation:(UIImage *)aImage { // No-op if the orientation is already correct
if (aImage.imageOrientation == UIImageOrientationUp)
return aImage; // We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransformIdentity; switch (aImage.imageOrientation) {
case UIImageOrientationDown:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break; case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, );
transform = CGAffineTransformRotate(transform, M_PI_2);
break; case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, , aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
} switch (aImage.imageOrientation) {
case UIImageOrientationUpMirrored:
case UIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, );
transform = CGAffineTransformScale(transform, -, );
break; case UIImageOrientationLeftMirrored:
case UIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.height, );
transform = CGAffineTransformScale(transform, -, );
break;
default:
break;
} // Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage), ,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
// Grr...
CGContextDrawImage(ctx, CGRectMake(,,aImage.size.height,aImage.size.width), aImage.CGImage);
break; default:
CGContextDrawImage(ctx, CGRectMake(,,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
} // And now we just create a new UIImage from the drawing context
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
来源:http://blog.csdn.net/iukey/article/details/7308433
使用UIImageView来播放动画.
-(void)viewDidLoad
{
[superviewDidLoad]; self.title=NSLocalizedString(@"ImagesTitle",@"");
//setupourUIImagewithagrouporarrayofimagestoanimate(orinourcaseaslideshow)
self.imageView.animationImages = [NSArrayarrayWithObjects:[UIImageimageNamed:@"scene1.jpg"],[UIImageimageNamed:@"scene2.jpg"],[UIImageimageNamed:@"scene3.jpg"],[UIImageimageNamed:@"scene4.jpg"],[UIImageimageNamed:@"scene5.jpg"], nil ]; imageView.animationDuration=5.0;
[self.imageViewstopAnimating]; //Settheappropriateaccessibilitylabels.
[self.imageViewsetIsAccessibilityElement:YES];
[self.imageViewsetAccessibilityLabel:self.title];
[self.slidersetAccessibilityLabel:NSLocalizedString(@"DurationSlider",@"")];
}