Iphone 转换后如何使用UIImageWriteToSavedPhotosAlbum保存图像

Iphone 转换后如何使用UIImageWriteToSavedPhotosAlbum保存图像,iphone,uiimage,uipickerview,cgcontext,Iphone,Uiimage,Uipickerview,Cgcontext,好的,我已经找遍了,似乎找不到我需要的答案-( 以下是我正在做的事情和我需要做的事情。我有一个UIImageView,我可以使用UIPangEstureRecognitor、UIRotationGestureRecognitor和UIPinchgEstureRecognitor进行转换。所有这些都很好。问题是在将这些转换保存到我的相册时出现的。结果不是我所期望的。到目前为止我正在使用的代码(可能非常不完整) 通过这种方法,我可以保存图像,但是坐标太远了。我可以看到我所做的任何缩放都正常。我看到了

好的,我已经找遍了,似乎找不到我需要的答案-(

以下是我正在做的事情和我需要做的事情。我有一个UIImageView,我可以使用UIPangEstureRecognitor、UIRotationGestureRecognitor和UIPinchgEstureRecognitor进行转换。所有这些都很好。问题是在将这些转换保存到我的相册时出现的。结果不是我所期望的。到目前为止我正在使用的代码(可能非常不完整)

通过这种方法,我可以保存图像,但是坐标太远了。我可以看到我所做的任何缩放都正常。我看到了我的旋转,但它似乎是向后的,平移太远了


坐标都是错的,就我的一生而言,我无法找出什么是错的。我承认我对Objective-C是新手,我以前从未使用过Quartz 2D,我对变换矩阵的理解是有限的。我知道我的变换不是在图像数据本身上,而是试图在不应用此方法的情况下保存图像上下文对它没有任何作用。所以有人能告诉我这一点吗?

在缩放之前,您通常需要进行翻译

您已经翻转了坐标系,但仍然指定了旧的x和y值。我认为您应该:

 CGFloat y = face.transform.tx;
 CGFloat x = face.transform.ty;
此代码中未定义
face
对象。如果该对象处于禁用状态,则其他所有对象都将处于禁用状态。如果该对象是属性,则应使用
self.face
参考表单以确保正确访问该对象

我建议最后执行缩放变换


如果这些都不起作用。请注释掉除一个变换外的所有变换,然后查看单个变换是否起作用。然后添加其他变换,直到失败。

如果在UIView中查看、变换、缩放图像,然后渲染层,这种工作会容易得多。这样可以避免以后的各种麻烦

比如:

CGSize vscaledSize = myOriginalImage.size;

//add in the scaled bits of the view
//scaling
CGFloat wratio = vscaledSize.width/self.pictureView.frame.size.width;
CGFloat vhightScaled = self.pictureView.frame.size.height * wratio;
vscaledSize.height = vhightScaled;

CGFloat hratio = vscaledSize.height/self.pictureView.frame.size.height;

//create context
UIGraphicsBeginImageContext(myOriginalImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context); //1 original context

// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, vscaledSize.height);
CGContextScaleCTM(context, 1.0, -1.0);

//original image
CGContextDrawImage(context, CGRectMake(0,0,vscaledSize.width,vscaledSize.height), myOriginalImage.CGImage);

CGContextRestoreGState(context);//1  restore to original;

//scale context to match view size
CGContextSaveGState(context); //1 pre-scaled size
CGContextScaleCTM(context, wratio, hratio);

//render 
[self.pictureView.layer renderInContext:context];

CGContextRestoreGState(context);//1  restore to pre-scaled size;

UIImage *exportImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
您会注意到,我翻转了图像渲染的坐标系,但将其恢复为UIView.layer渲染的原始坐标系

希望这有帮助

CGSize vscaledSize = myOriginalImage.size;

//add in the scaled bits of the view
//scaling
CGFloat wratio = vscaledSize.width/self.pictureView.frame.size.width;
CGFloat vhightScaled = self.pictureView.frame.size.height * wratio;
vscaledSize.height = vhightScaled;

CGFloat hratio = vscaledSize.height/self.pictureView.frame.size.height;

//create context
UIGraphicsBeginImageContext(myOriginalImage.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context); //1 original context

// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, vscaledSize.height);
CGContextScaleCTM(context, 1.0, -1.0);

//original image
CGContextDrawImage(context, CGRectMake(0,0,vscaledSize.width,vscaledSize.height), myOriginalImage.CGImage);

CGContextRestoreGState(context);//1  restore to original;

//scale context to match view size
CGContextSaveGState(context); //1 pre-scaled size
CGContextScaleCTM(context, wratio, hratio);

//render 
[self.pictureView.layer renderInContext:context];

CGContextRestoreGState(context);//1  restore to pre-scaled size;

UIImage *exportImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();