2013-04-12 44 views
1

我正在處理相機相關的應用程序。在這裏我正在拍攝來自相機的圖像,需要裁剪,然後將其修復爲圖像視圖。裁剪圖像時我使用的是OpenGL。我的問題是我裁剪後的圖像旋轉了180度。但這並非一直在發生。有些時候我正在獲取原始圖像本身。如何處理IOS圖像裁剪中的圖像旋轉問題

-(void)showResult 
{ 
    NSLog(@"showResult called"); 

    UIImage *imageCrop; 
    float scaleCrop; 
    if (_sourceImage.size.width >= IMAGEWIDTH) 
    { 
     scaleCrop = IMAGEWIDTH/_sourceImage.size.width; 
     imageCrop = [ImageCropViewController scaleImage:_sourceImage with:CGSizeMake(_sourceImage.size.width*scaleCrop, _sourceImage.size.height*scaleCrop)]; 
    } 
    else 
    { 
     scaleCrop = 1; 
     imageCrop = _sourceImage; 
    } 


    float scale = _sourceImage.size.width/resizeImage.size.width * 2; 
    IplImage *iplImage = [ImageCropViewController CreateIplImageFromUIImage:imageCrop] ; 
    Quadrilateral rectan; 

    rectan.point[0].x = _touchLayer.rectan.pointA.x*scale*scaleCrop; 
    rectan.point[0].y = _touchLayer.rectan.pointA.y*scale*scaleCrop; 

    rectan.point[1].x = _touchLayer.rectan.pointB.x*scale*scaleCrop; 
    rectan.point[1].y = _touchLayer.rectan.pointB.y*scale*scaleCrop; 

    rectan.point[2].x = _touchLayer.rectan.pointC.x*scale*scaleCrop; 
    rectan.point[2].y = _touchLayer.rectan.pointC.y*scale*scaleCrop; 

    rectan.point[3].x = _touchLayer.rectan.pointD.x*scale*scaleCrop; 
    rectan.point[3].y = _touchLayer.rectan.pointD.y*scale*scaleCrop; 

    IplImage* dest = cropDoc2(iplImage,rectan); 

    IplImage *image = cvCreateImage(cvGetSize(dest), IPL_DEPTH_8U, dest->nChannels); 
    cvCvtColor(dest, image, CV_BGR2RGB); 
    cvReleaseImage(&dest); 

    tempImage = [ImageCropViewController UIImageFromIplImage:image withImageOrientation:_sourceImage.imageOrientation]; 
    [self crop:tempImage]; 
    cvReleaseImage(&image); 

} 

之後,下面的方法被稱爲

+ (UIImage *)UIImageFromIplImage:(IplImage *)image withImageOrientation:(UIImageOrientation)orientation 
{ 
    NSLog(@"UIImageFromIplImage called"); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize]; 
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data); 
    CGImageRef imageRef = CGImageCreate(image->width, image->height, image->depth, image->depth * image->nChannels, image->widthStep, colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault, provider, NULL, false, kCGRenderingIntentDefault); 
    UIImage *ret = [UIImage imageWithCGImage:imageRef scale:1 orientation:orientation]; 
    CGImageRelease(imageRef); 
    CGDataProviderRelease(provider); 
    CGColorSpaceRelease(colorSpace); 
    return ret; 
} 

那麼我旋轉圖像按我的要求,因爲

-(void)crop:(UIImage*)image 

{ 
    NSLog(@"crop called"); 
    //Adjust the image size, to scale the image to 1013 of width 
    float targetWidth = 1009.0f; 
    float scale = targetWidth/image.size.width; 
    float scaleheight = image.size.height * scale; 
    UIImage *imageToSent = [ImageCropViewController scaleImage:image  with:CGSizeMake(targetWidth, scaleheight)]; 


if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) 
{ 

    NSLog(@"###########Image orientation is UIInterfaceOrientationLandscapeLeft###########"); 
    imageToSent = [[UIImage alloc] initWithCGImage:imageToSent.CGImage scale:1.0f orientation:UIImageOrientationDown]; 

} 

NSData *imageData = UIImageJPEGRepresentation(imageToSent,0.75); 
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0]; 
NSString *caldate = [now description]; 
appDelegate.imagefilePath= [NSString stringWithFormat:@"%@/%@.jpg", DOCUMENTS_FOLDER,caldate]; 
[imageData writeToFile:appDelegate.imagefilePath atomically:YES]; 

appDelegate.cropimage=imageToSent; 
} 

我沒有得到它不見了wrong.It是殺了我的時間,請幫助我。

在此先感謝

+0

刪除此if和嘗試如果([UIApplication sharedApplication] .statusBarOrientation == UIInterfaceOrientationLandscapeLeft) {爲什麼你用這個? – Spynet

回答

3

我有此代碼進行裁剪。你爲什麼使用openGL。這是我的代碼。

- (UIImage *)cropImage : (UIImage*)myImage withRect:(CGRect)rect 
{ 
     CGImageRef imageRef = CGImageCreateWithImageInRect([myImage CGImage], rect); 
     UIImage *img = [UIImage imageWithCGImage:imageRef]; 
     CGImageRelease(imageRef); 
     return img; 
} 

只要你想裁剪圖像,就調用這個方法。此外,當您從相機或圖庫(從本機相機應用拍攝的圖像)拍攝圖像時,您將看到旋轉的圖像。使用此代碼將其恢復爲原始圖像。

//----rotate image if picked from gallery or camera----// 
- (UIImage *)scaleAndRotateImage:(UIImage *)image { 

    NSLog(@"scaleAndRotateImage"); 
    static int kMaxResolution = 640; // this is the maximum resolution that you want to set for an image. 

    CGImageRef imgRef = image.CGImage; 
    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGAffineTransform transform = CGAffineTransformIdentity; 
    CGRect bounds = CGRectMake(0, 0, width, height); 
    if (width > kMaxResolution || height > kMaxResolution) { 
     CGFloat ratio = width/height; 
     if (ratio > 1) { 
      bounds.size.width = kMaxResolution; 
      bounds.size.height = bounds.size.width/ratio; 
     } else { 
      bounds.size.height = kMaxResolution; 
      bounds.size.width = bounds.size.height * ratio; 
     } 
    } 

    CGFloat scaleRatio = bounds.size.width/width; 
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); 
    CGFloat boundHeight; 
    UIImageOrientation orient = image.imageOrientation; 
    switch(orient) { 
     case UIImageOrientationUp: 
      transform = CGAffineTransformIdentity; 
      break; 
     case UIImageOrientationUpMirrored: 
      transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      break; 
     case UIImageOrientationDown: 
      transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height); 
      transform = CGAffineTransformRotate(transform, M_PI); 
      break; 
     case UIImageOrientationDownMirrored: 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.height); 
      transform = CGAffineTransformScale(transform, 1.0, -1.0); 
      break; 
     case UIImageOrientationLeftMirrored: 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width); 
      transform = CGAffineTransformScale(transform, -1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 
     case UIImageOrientationLeft: 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(0.0, imageSize.width); 
      transform = CGAffineTransformRotate(transform, 3.0 * M_PI/2.0); 
      break; 
     case UIImageOrientationRightMirrored: 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeScale(-1.0, 1.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 
     case UIImageOrientationRight: 
      boundHeight = bounds.size.height; 
      bounds.size.height = bounds.size.width; 
      bounds.size.width = boundHeight; 
      transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0); 
      transform = CGAffineTransformRotate(transform, M_PI/2.0); 
      break; 
     default: 
      [NSException raise:NSInternalInconsistencyException 
         format:@"Invalid image orientation"]; 
    } 

    UIGraphicsBeginImageContext(bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
     CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
     CGContextTranslateCTM(context, -height, 0); 
    } else { 
     CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
     CGContextTranslateCTM(context, 0, -height); 
    } 
    CGContextConcatCTM(context, transform); 
    CGContextDrawImage(UIGraphicsGetCurrentContext(), 
         CGRectMake(0, 0, width, height), imgRef); 
    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return returnImage; 
} 

希望我幫了忙。

+0

我們試過這個,但是我們需要爲cropping lines/points設置動畫效果,這就是爲什麼我們使用OpenGl – Goutham

+0

設置動畫剪枝線的意思? – Swati

+0

邊緣點應該在任何方向上移動,不僅在方形/矩形中 – Goutham