2011-07-07 104 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)]; 
}]; 

我希望这能帮助呢!

干杯。