2013-01-11 46 views
0

我擁有專輯表中的照片數據庫。我得到陣列中的所有圖像並顯示在表格中。 問題是很少的圖像旋轉90度。iphone桌面上的某些圖像會自動旋轉

這是cellForRow方法。

AlbumInformation * temp=[albumInfoArray objectAtIndex:indexPath.row]; 
cell.albumPicImageView.image=temp.albumPictureClass; 
return cell; 

我得到所有圖像,但很少旋轉。

請幫忙。

在此先感謝

回答

0

UIImage有一個賦值屬性「imageOrientation」; 所以這種方法可以幫助你

  • (的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, 0); 
         transform = CGAffineTransformRotate(transform, M_PI_2); 
         break; 
    
        case UIImageOrientationRight: 
        case UIImageOrientationRightMirrored: 
         transform = CGAffineTransformTranslate(transform, 0, 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, 0); 
         transform = CGAffineTransformScale(transform, -1, 1); 
         break; 
    
        case UIImageOrientationLeftMirrored: 
        case UIImageOrientationRightMirrored: 
         transform = CGAffineTransformTranslate(transform, aImage.size.height, 0); 
         transform = CGAffineTransformScale(transform, -1, 1); 
         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), 0, 
                 CGImageGetColorSpace(aImage.CGImage), 
                 CGImageGetBitmapInfo(aImage.CGImage)); 
    CGContextConcatCTM(ctx, transform); 
    switch (aImage.imageOrientation) { 
        case UIImageOrientationLeft: 
        case UIImageOrientationLeftMirrored: 
        case UIImageOrientationRight: 
        case UIImageOrientationRightMirrored: 
         // 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 
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 
    UIImage *img = [UIImage imageWithCGImage:cgimg]; 
    CGContextRelease(ctx); 
    CGImageRelease(cgimg); 
    return img; 
    

    }

+1

我得到的圖像定位爲所有圖像,但一些圖像旋轉我試過如果(temp.albumPictureClass == UIImageOrientationUP){NSLog(@「Up」);}所有圖像打印此消息 – Durgaprasad

+0

這意味着您的圖像方向都是「向上」。所以我認爲你可以找到行哪個圖像旋轉90度,然後通過代碼旋轉它們......我的英語非常糟糕,所以我不能確定你能理解我說的話。 - !抱歉! – iuser1969782

+0

是的,我明白了。我接受了你的方法。這是完美的。謝謝。 – Durgaprasad

0

請參閱下面的示例。在以下兩種情況下的PhotoDisplayViewController.m測試中,您將自動得到答案,無需解釋任何人。

案例1:UIImageOrientation

ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation]; 

    UIImage *fullScreenImage = [UIImage imageWithCGImage:[assetRepresentation fullScreenImage] scale:[assetRepresentation scale] orientation:(UIImageOrientation)[assetRepresentation orientation]]; 

案例2:用了UIImageOrientation

ALAssetRepresentation *assetRepresentation = [asset defaultRepresentation]; 

    UIImage *fullScreenImage = [UIImage imageWithCGImage:[assetRepresentation fullScreenImage] scale:[assetRepresentation scale] orientation:nil]; 

當您從您的閱讀主張有一定的取向就像下面

UIImageOrientationUp,   // default orientation 
    UIImageOrientationDown,   // 180 deg rotation 
    UIImageOrientationLeft,   // 90 deg CCW 
    UIImageOrientationRight,   // 90 deg CW 
    UIImageOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip 
    UIImageOrientationDownMirrored, // horizontal flip 
    UIImageOrientationLeftMirrored, // vertical flip 
    UIImageOrientationRightMirrored, // vertical flip 

所以請下載通過這個示例演示

MyImagePicker Demo

注: ,如果你想從那麼所有的專輯,你需要在RootViewController.m改變代碼如下

NSUInteger groupTypes = ALAssetsGroupAlbum | ALAssetsGroupEvent | ALAssetsGroupFaces|ALAssetsGroupLibrary|ALAssetsGroupSavedPhotos|ALAssetsGroupPhotoStream; 
顯示