2013-03-27 130 views
0

我正在嘗試調整圖像大小,但無法正常工作。我有320和480的高度,我想調整它的寬度1024和高度1024我使用此代碼來調整圖像在Iphone中調整圖像大小

- (UIImage *)resizeImageToSize:(CGSize)targetSize 
{ 
    UIImage *sourceImage = [self saveImage:0]; 

    UIImage *newImage = nil; 

    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 

    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 

    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) { 

     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor < heightFactor) 
      scaleFactor = widthFactor; 
     else 
      scaleFactor = heightFactor; 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // make image center aligned 
     if (widthFactor < heightFactor) 
     { 
      thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
     else if (widthFactor > heightFactor) 
     { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
    } 

    UIGraphicsBeginImageContext(targetSize); 
    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 
    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    if(newImage == nil) 
     NSLog(@"could not scale image"); 

    return newImage ; 
} 

我沒有得到的圖像大小我想要的圖像寬度。

這是我從320x480調整圖像大小到1280x1024時得到的圖像 圖像大小正在增加,但圖像不是。我想讓那輛車在白色背景上延伸enter image description here

回答

2

要調整和保持縱橫比試試這個....這是非常類似於上面的方法:

- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSizeWithSameAspectRatio:(CGSize)targetSize; 
{ 
    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 
    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 
    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) { 
     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor > heightFactor) { 
      scaleFactor = widthFactor; // scale to fit height 
     } 
     else { 
      scaleFactor = heightFactor; // scale to fit width 
     } 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // center the image 
     if (widthFactor > heightFactor) { 
      thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
     else if (widthFactor < heightFactor) { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
    }  

    CGImageRef imageRef = [sourceImage CGImage]; 
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef); 

    if (bitmapInfo == kCGImageAlphaNone) { 
     bitmapInfo = kCGImageAlphaNoneSkipLast; 
    } 

    CGContextRef bitmap; 

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) { 
     bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    } else { 
     bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo); 

    } 

    // In the right or left cases, we need to switch scaledWidth and scaledHeight, 
    // and also the thumbnail point 
    if (sourceImage.imageOrientation == UIImageOrientationLeft) { 
     thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); 
     CGFloat oldScaledWidth = scaledWidth; 
     scaledWidth = scaledHeight; 
     scaledHeight = oldScaledWidth; 

     CGContextRotateCTM (bitmap, M_PI_2); // + 90 degrees 
     CGContextTranslateCTM (bitmap, 0, -targetHeight); 

    } else if (sourceImage.imageOrientation == UIImageOrientationRight) { 
     thumbnailPoint = CGPointMake(thumbnailPoint.y, thumbnailPoint.x); 
     CGFloat oldScaledWidth = scaledWidth; 
     scaledWidth = scaledHeight; 
     scaledHeight = oldScaledWidth; 

     CGContextRotateCTM (bitmap, -M_PI_2); // - 90 degrees 
     CGContextTranslateCTM (bitmap, -targetWidth, 0); 

    } else if (sourceImage.imageOrientation == UIImageOrientationUp) { 
     // NOTHING 
    } else if (sourceImage.imageOrientation == UIImageOrientationDown) { 
     CGContextTranslateCTM (bitmap, targetWidth, targetHeight); 
     CGContextRotateCTM (bitmap, -M_PI); // - 180 degrees 
    } 

    CGContextDrawImage(bitmap, CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledWidth, scaledHeight), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage* newImage = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); 
    CGImageRelease(ref); 

    return newImage; 
} 

如果你想使用它們,你會做這樣的事情:

UIImage* myThumbnail = ...; // Get some image 
NSData* imageData = UIImagePNGRepresentation(myThumbnail); 

將其保存到磁盤,(說成文件目錄):

// Give a name to the file 
NSString* imageName = @"MyImage.png"; 

// Now, we have to find the documents directory so we can save it 
// Note that you might want to save it elsewhere, like the cache directory, 
// or something similar. 
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString* documentsDirectory = [paths objectAtIndex:0]; 

// Now we get the full path to the file 
NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName]; 

// and then we write it out 
[imageData writeToFile:fullPathToFile atomically:NO]; 
+0

當我返回圖像並保存它。它沒有儲蓄。和日誌顯示寬度0高度0.對於該newImage – Zohaib 2013-03-27 08:24:41

0

這很簡單

- (void)viewDidLoad{ 
    self.testImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"youImage.png"]]; 
    self.testImageView.frame = CGRectMake(100, 100, 320, 480); 
    [self.view addSubview:self.testImageView]; 
} 

-(void)resizeImageToSize:(CGSize)targetSize 
{ 
    self.testImageView.fram = targetSize; 
} 
+0

我想調整圖像而不是imageview。所以圖像不具有框架屬性。 – Zohaib 2013-03-27 08:28:08

1
if(image) 
    { 
     UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); 
     [image drawInRect:CGRectMake(0, 0, newWidth, newHeight)]; 
     imageAfterResize = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     ; 
    } 

這是不夠用?