2011-06-20 31 views
0

基本上,我制作的应用程序涉及用户拍摄照片,或者选择已在设备上的照片,然后在图像上放置覆盖图。iPhone SDK:在另一个图像上保存一个图像的问题

因此,除了一件事情之外,我似乎已经编码好所有东西,在用户选择覆盖并定位之后,保存时覆盖层的大小发生了变化,而x和y值似乎是正确的。

所以这是我用来添加叠加(“图像”作为用户照片)代码:

​​

这是用来保存生成的图像的代码:

UIGraphicsBeginImageContext(image.image.size); 

// Draw the users photo 
[image.image drawInRect:CGRectMake(0, 0, image.image.size.width, image.image.size.height)]; 

// Draw the overlay 
float xx = (overlay.center.x); 
float yy = (overlay.center.y); 

CGRect aaFrame = overlay.frame; 
float width = aaFrame.size.width; 
float height = aaFrame.size.height; 


[overlay.image drawInRect:CGRectMake(xx, yy, width, height)]; 
    UIGraphicsEndImageContext(); 

有什么帮助吗?谢谢

回答

2

问题是,您正在使用图像的大小,而不是图像视图的帧大小。图片似乎比图片视图大得多,因此当您使用图片大小时,其他图片的大小最终会比较小,但它仍然是正确的大小。您可以通过修改段将 -

UIGraphicsBeginImageContext(image.frame.size); 

// Draw the users photo 
[image.image drawInRect:CGRectMake(0, 0, image.frame.size.width, image.frame.size.height)]; 
[overlay.image drawInRect:overlay.frame]; 

UIImage * resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 

避免质量

的损失,而上述方法会导致分辨率的损失,企图拉拢父图像在其适当的分辨率可能有不必要的影响就其子女形象而言,即如果覆盖层本身不具有高分辨率,那么它可以结束有弹性。不过你可以试试这个代码,以绘制在父图像的分辨率(未经测试,让我知道,如果你的问题) -

float verticalScale = image.image.size.height/image.frame.size.height; 
float horizontalScale = image.image.size.width/image.frame.size.width; 

CGRect overlayFrame = overlay.frame; 
overlayFrame.origin.x *= horizontalScale; 
overlayFrame.origin.y *= verticalScale; 
overlayFrame.size.width *= horizontalScale; 
overlayFrame.size.height *= verticalScale; 

UIGraphicsBeginImageContext(image.image.size); 

// Draw the users photo 
[image.image drawInRect:CGRectMake(0, 0, image.image.size.width, image.image.size.height)]; 
[overlay.image drawInRect:overlayFrame]; 

UIImage * resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
+0

谢谢,这个作品真的很好,但它是一个关于质量的损失耻辱。我为.image方法而不是.frame进行研究的原因之一是它会保持图像的真实分辨率。是否有另一种方式来添加覆盖作为子视图,但它代表其在子视图中的真实大小? – Sam

+0

@Jessica,我已经添加了适合您需求的代码。它未经测试。你能检查它是否有效吗? –

+0

对不起,延迟,我刚刚测试它,它非常有效,非常感谢您的帮助! – Sam

相关问题