【问题标题】:Resize image in Iphone在 Iphone 中调整图像大小
【发布时间】:2014-09-01 19:09:09
【问题描述】:

我正在尝试调整图像大小,但它无法正常工作。我有一个图像宽度 320 和高度 480 我想将其调整为宽度 1024 和高度 1024。我正在使用此代码调整图像大小

- (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 时得到的结果 图像大小在增加,但图像没有。我想把那辆车拉到白色背景上。

【问题讨论】:

    标签: iphone ios objective-c xcode


    【解决方案1】:

    你想调整大小并保持纵横比试试这个....它与上面的方法非常相似:

    - (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。对于该 newImage
    【解决方案2】:
    if(image)
        {
            UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
            [image drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
             imageAfterResize = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            ;
        }
    

    这对你来说够了吗?

    【讨论】:

      【解决方案3】:

      很简单

      - (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;
      }
      

      【讨论】:

      • 我想调整图像大小而不是图像视图。所以图像没有框架属性。
      猜你喜欢
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 2013-01-11
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 2017-01-16
      相关资源
      最近更新 更多