2013-10-14 34 views
0

我正在開發iphone應用程序,用戶可以在其中添加自定義照片。現在我需要在iOS7中將此照片轉換爲5英寸高4.5英寸的寬度。Rsize照片在iOS 7+中以5英寸高4.5英寸寬度

我正在爲下視圖的截圖照片&標籤添加到它結合如下

-(UIImage*)customizedImageMain 
{ 
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
     UIGraphicsBeginImageContextWithOptions(self.containerView.bounds.size, NO, [UIScreen mainScreen].scale); 
    else 
     UIGraphicsBeginImageContext(self.containerView.bounds.size); 
    [self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return finalImage; 
} 

我從iPod touch的檢查圖像與4" 畫面,圖像大小,我得到從上面代碼幾乎4.445" 寬& 7.889" 的高度。

現在我怎樣可以調整此圖片精確5" 高度& 4.5" 寬度無關的裝置,其中應用程序正在運行?

提前致謝。

更新的代碼按輸入電壓的解決方案

-(UIImage*)customizedImageFirst 
{ 
    // We want 5" * 4.5" image which can be represented as 1630 * 1458 
    // 1630 pixels = 5 inch * (326 pixels/1 inch) 
    // 1458 pixels = 4.5 inch * (326 pixels/1 inch) in terms of pixels. 
    CGSize requiredImageSize = CGSizeMake(1458.0,1630.0); 
    UIGraphicsBeginImageContext(requiredImageSize); 
    [self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    [combinedImage drawInRect:CGRectMake(0,0,requiredImageSize.width,requiredImageSize.height)]; 
    UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return finalImage; 
} 

使用上面的代碼我得到this result

現在,如果我使用代碼沒有考慮UILabels添加到自定義如下

-(UIImage*)customizedImageSecond // If I use your code 
{ 
    // We want 5" * 4.5" image which can be represented as 1630 * 1458 
    // 1630 pixels = 5 inch * (326 pixels/1 inch) 
    // 1458 pixels = 4.5 inch * (326 pixels/1 inch) in terms of pixels. 
    CGSize requiredImageSize = CGSizeMake(1458.0,1630.0); 
    UIGraphicsBeginImageContext(requiredImageSize); 
    [self.myImageView.image drawInRect:CGRectMake(0,0,requiredImageSize.width,requiredImageSize.height)]; 
    UIImage* finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return finalImage; 
} 

,然後我得到this image

我的UI層次的UIViewController

主視圖。我在主視圖中添加了另一個UIView(containerView)。在這個containerView中,我添加了我的UIImageView & UILabels。所以得到帶有添加標籤的自定義圖像我正在使用containerView的屏幕截圖。

回答

1

這是我通常用來將圖像縮放到所需大小的自定義方法。該方法以newSize例如:CGSize newSize = CGSizeMake(30.0, 30.0);)爲參數之一。這裏,30.0像素的值。

+(UIImage*)imageWithImage:(UIImage*)image 
       scaledToSize:(CGSize)newSize 
{ 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; 
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

所以,你可以在使用上述方法的像素方面縮放圖像。但是,您想根據英寸來縮放圖像。顯示設備具有可顯示的有限數量的像素以及有限的顯示它們的空間。

Pixel Density是設備的分辨率的測量,並且可以在PPI(每英寸像素數)PPCM(每釐米像素數)方面進行計算。

在你的情況,你想5" * 4.5"圖像可以在像素方面表示爲1630 * 1458 (1630 pixels = 5 inch * (326 pixels/1 inch), 1458 pixels = 4.5 inch * (326 pixels/1 inch))

注:PPI爲蘋果iPhone和iPod Touch上面的第四代是

所以,如果您按比例縮放圖像大小爲1630 * 1458那麼你會得到的圖像5"" * 4.5"。爲此,您必須通過上述方法中的CGSize newSize = CGSizeMake(1630.0, 1458.0);

+0

感謝Vin爲您的解決方案和很好的解釋。我非常感謝你的解釋。我會檢查這個&讓你知道。 – iOSAppDev

+0

如果你可以看到我正在使用'[self.containerView.layer renderInContext:UIGraphicsGetCurrentContext()]''這樣我就可以結合在UIImage上添加的圖像和標籤。如果我遵循你的解決方案,我不會廢除正確的輸出。你能告訴我發生了什麼事嗎?請看看我的更新。謝謝 – iOSAppDev

+0

@iOSAppDev:你面臨的問題是什麼?你沒有得到什麼?爲什麼你不能直接使用我的代碼? – Bhavin

相關問題