2011-07-07 102 views
1

我想用UIslider控件旋轉圖像。旋轉圖像並將圖像保存爲旋轉狀態

我已經做到了與以下功能

 
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration 
       curve:(int)curve degrees:(CGFloat)degrees 
{ 
    // Setup the animation 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:duration]; 
    [UIView setAnimationCurve:curve]; 
    [UIView setAnimationBeginsFromCurrentState:YES];

// The transform matrix CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees)); image.transform = transform; // Commit the changes [UIView commitAnimations];}

使用此功能,將工作完美 但問題是,我無法保存旋轉圖像的參考。 我必須使用旋轉的圖像進行進一步處理。

那麼我怎樣才能保存在旋轉位置的圖像?

請幫助我在這個問題上

感謝

回答

3

我找到了解決辦法我的問題

使用這種方法,下面爲這

- (UIImage*)upsideDownBunny:(CGFloat)radians withImage:(UIImage*)testImage { 
    __block CGImageRef cgImg; 
    __block CGSize imgSize; 
    __block UIImageOrientation orientation; 
    dispatch_block_t createStartImgBlock = ^(void) { 
     // UIImages should only be accessed from the main thread 

     UIImage *img =testImage 
     imgSize = [img size]; // this size will be pre rotated 
     orientation = [img imageOrientation]; 
     cgImg = CGImageRetain([img CGImage]); // this data is not rotated 
    }; 
    if([NSThread isMainThread]) { 
     createStartImgBlock(); 
    } else { 
     dispatch_sync(dispatch_get_main_queue(), createStartImgBlock); 
    } 
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
    // in iOS4+ you can let the context allocate memory by passing NULL 
    CGContextRef context = CGBitmapContextCreate(NULL, 
               imgSize.width, 
               imgSize.height, 
               8, 
               imgSize.width * 4, 
               colorspace, 
               kCGImageAlphaPremultipliedLast); 
    // rotate so the image respects the original UIImage's orientation 
    switch (orientation) { 
     case UIImageOrientationDown: 
      CGContextTranslateCTM(context, imgSize.width, imgSize.height); 
      CGContextRotateCTM(context, -radians); 
      break; 
     case UIImageOrientationLeft: 
      CGContextTranslateCTM(context, 0.0, imgSize.height); 
      CGContextRotateCTM(context, 3.0 * -radians/2.0); 
      break; 
     case UIImageOrientationRight: 
      CGContextTranslateCTM(context,imgSize.width, 0.0); 
      CGContextRotateCTM(context, -radians/2.0); 
      break; 
     default: 
      // there are mirrored modes possible 
      // but they aren't generated by the iPhone's camera 
      break; 
    } 
    // rotate the image upside down 

    CGContextTranslateCTM(context, +(imgSize.width * 0.5f), +(imgSize.height * 0.5f)); 
    CGContextRotateCTM(context, -radians); 
    //CGContextDrawImage(context, CGRectMake(0.0, 0.0, imgSize.width, imgSize.height), cgImg); 
    CGContextDrawImage(context, (CGRect){.origin.x = -imgSize.width* 0.5f , .origin.y = -imgSize.width* 0.5f , .size.width = imgSize.width, .size.height = imgSize.width}, cgImg); 
    // grab the new rotated image 
    CGContextFlush(context); 
    CGImageRef newCgImg = CGBitmapContextCreateImage(context); 
    __block UIImage *newImage; 
    dispatch_block_t createRotatedImgBlock = ^(void) { 
     // UIImages should only be accessed from the main thread 
     newImage = [UIImage imageWithCGImage:newCgImg]; 
    }; 
    if([NSThread isMainThread]) { 
     createRotatedImgBlock(); 
    } else { 
     dispatch_sync(dispatch_get_main_queue(), createRotatedImgBlock); 
    } 
    CGColorSpaceRelease(colorspace); 
    CGImageRelease(newCgImg); 
    CGContextRelease(context); 
    return newImage; 
} 

調用此方法

UIImage *rotated2 = [self upsideDownBunny:rotation]; 

其中旋轉sliderValue

之間0到360,現在我們可以節省旋轉狀態。

+0

感謝darshan它的工作完美。 – AJPatel

+0

很高興聽到它的作品完美.Thaks – darshan

0

一種方法來進一步處理按順序應用多個變換,像:

CGAffineTransform transform = CGAffineTransformScale(previousTransform, newScale, newScale); 
在這種情況下

,你會縮放適用於你的旋轉圖像。

如果您需要保存此信息以便稍後能夠重新進行轉換,則可以簡單地存儲旋轉角度,縮放因子(在我的示例中)以及再次構建轉換。

你也可以考慮將你的CGAffineTransform存儲在你的班級或其他機構的ivar中。

編輯:

如果通過保存你的意思是保存到一個文件,您可以將您的看法與此代碼轉換爲圖像:

NSData *data; 
NSBitmapImageRep *rep; 
rep = [self bitmapImageRepForCachingDisplayInRect:[self frame]]; 
[self cacheDisplayInRect:[self frame] toBitmapImageRep:rep]; 
data = [rep TIFFRepresentation]; 

然後保存的NSData到文件

對於PNG:

UIGraphicsBeginImageContext(self.bounds.size); 
[self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage* image1 = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
NSData *imageData = UIImagePNGRepresentation(image1); 
[imageData writeToFile:filePath atomically:YES]; 
+0

我在旋轉圖像時沒有問題,但問題是我必須保存旋轉的圖像。所以我該怎麼做? – darshan

+0

保存是什麼意思? – sergio

+0

將圖像保存在旋轉位置。 – darshan

0

您可以將旋轉的圖像渲染到另一個圖像中:

UIGraphicsBeginImageContext(aCGRectToHoldYourImage); 
CALayer* layer = myRotatedImageView.layer; 

[layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

,然後從得到的PNG數據,並將其保存到一個文件

NSData* myPNGData = [viewImage UIImagePNGRepresentation]; 
[myPNGData writeToFile:@"aFileName.png" atomically:YES]; 

限制性條文,我輸入到StackOverflow上這個,不是一個編譯器:-)

0

保存UIImageView的變換值,並在下次要使用此UIImageView或甚至在UIImageView中應用此變換值。

+0

所有帽子多? –

1

我想上面的代碼,它的工作,但只有一個正方形的圖像,這裏是另一種可行的解決方案,這將重繪圖像,並保持正確的寬度/高度:

- (UIImage *) rotatedImage:(UIImage *)imageRotation and:(CGFloat) rotation 
{ 
// Calculate Destination Size 
CGAffineTransform t = CGAffineTransformMakeRotation(rotation); 
CGRect sizeRect = (CGRect) {.size = imageRotation.size}; 
CGRect destRect = CGRectApplyAffineTransform(sizeRect, t); 
CGSize destinationSize = destRect.size; 

// Draw image 
UIGraphicsBeginImageContext(destinationSize); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextTranslateCTM(context, destinationSize.width/2.0f, destinationSize.height/2.0f); 
CGContextRotateCTM(context, rotation); 
[imageRotation drawInRect:CGRectMake(-imageRotation.size.width/2.0f, -imageRotation.size.height/2.0f, imageRotation.size.width, imageRotation.size.height)]; 

// Save image 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return newImage; 

}

這裏的旋轉參數是以弧度給出的。您可以通過添加此功能,您上面進行轉換(或你.M文件直接)

#define M_PI 3.14159265358979323846264338327950288 /* pi */ 
#define DEGREES_TO_RADIANS(angle) (angle/180.0 * M_PI) 

最後,我稱這與動畫:

[UIView animateWithDuration:0.5f delay:0 options:UIViewAnimationCurveEaseIn animations:^{ 
    //Only used for the ANIMATION of the uiimage 
    imageView.transform = CGAffineTransformRotate(editorPad.transform, DEGREES_TO_RADIANS(90)); 
}completion:^(BOOL finished) { 
    // Workaround : Need to set previous transformation back or the next step will turn the image more 
    imageView.transform = CGAffineTransformRotate(imageView.transform, DEGREES_TO_RADIANS(-90)); 
    imageView.image = [self imageView.image and:DEGREES_TO_RADIANS(90)]; 
}]; 

我希望這能幫助呢!

乾杯。