2012年2月9日 星期四

ios 重繪等比例縮放照片

選取手機相簿裡尺寸較為大張的照片
且push或presentModalViewController時,
在滑動整個畫面的過程會出現非常不順的情形
原因在於圖片的size太大了
解決方法就是重繪照片並且resize就可以囉
- (void) resizeImage:(UIImage *)newImage 
{    
    float maxSizeWidth = 640;
    float maxSizeHeight = 640;
    
    if (newImage.size.width > maxSizeWidth || newImage.size.height > maxSizeHeight) 
    {
        float actualHeight = newImage.size.height;
        float actualWidth = newImage.size.width;
        
        float imgRatio = actualWidth/actualHeight;
        float maxRatio = maxSizeWidth/maxSizeHeight;
  
        if(imgRatio!=maxRatio)
        {
             if(imgRatio < maxRatio)
             {
                imgRatio = maxSizeHeight / actualHeight;
                actualWidth = imgRatio * actualWidth;
                actualHeight = maxSizeHeight;
             }
             else
             {
                imgRatio = maxSizeWidth / actualWidth;
                actualHeight = imgRatio * actualHeight;
                actualWidth = maxSizeWidth;
             }
         }        
         CGRect rect = CGRectMake(0, 0, (int)actualWidth, (int)actualHeight);
         UIGraphicsBeginImageContext(rect.size);
         [newImage drawInRect:rect];
         uploadImageView.image = UIGraphicsGetImageFromCurrentImageContext();
         UIGraphicsEndImageContext();
     }
}

沒有留言:

張貼留言