2012-01-10 72 views
1

我想缩小图片我从用户touchesmoved从照片库中获得类似的方式,如当我们用相机使用UIImagepicker setEditing到的方法(或像相机应用程序)拍照时。在iPhone中从照片库中缩放并保存UIImage?

我想使用下面的方法传递一些基于touchesmoved的参数,但我没有得到所需的效果?我可能做错了什么?

-(UIImage*)scaleToSize:(UIImage *)img:(CGSize)size 
{ 


// Create a bitmap graphics context 
    // This will also set it as the current context 
    UIGraphicsBeginImageContext(size); 

// Draw the scaled image in the current context 
[img drawInRect:CGRectMake(0, 0, size.width, size.height)]; 

// Create a new image from current context 
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 

// Pop the current context from the stack 
UIGraphicsEndImageContext(); 

// Return our new scaled image 
return scaledImage; 
} 

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UIImage *img = [self scaleToSize:imgView.image:CGSizeMake(touch1.x,touch1.y)]; 
     imgView.image=img; 
} 

另外我怎样才能保存缩放后的图像一次以某种方式我缩放?

+0

你有没有试图把固定值到您的来电scaleToSize:IMG:以检查它是否是你接触逻辑不正确或你的方法是什么?你现在得到什么结果? – 2012-01-10 08:48:39

+0

我已添加修复值,它与修复值一起工作..问题不是很多值我给任何值扭曲的图像,而不是缩放它 – hemant 2012-01-10 09:21:33

回答

1

基于您的评论,图像会扭曲,因为它会将图像绘制到指定的矩形中,并且如果新尺寸与原始图像的纵横比(宽度/高度)不一致,则会出现扭曲。

你需要一些逻辑,以确保您的新高度和宽度具有相同的纵横比,例如:

CGFloat newHeight = imageView.frame.size.height * size.width/imageView.frame.size.width; 

如果你让你的显卡方面size.width和newHeight然后绘制图像到这个它会保持纵横比。

您可能希望在其中放置一些额外的逻辑,以根据给定宽度的高度或新高度创建新宽度,具体取决于哪个维度是最大的更改。

希望这有助于

戴夫

相关问题