裁剪的图像在iPhone中变得越来越大?

时间:2013-04-10 13:58:34

标签: iphone ios objective-c ipad

我有一个应用程序,我将(360,480)的图像缩放到(320,480),然后裁剪图像。它工作正常。但是当我们得到图像时,它得到一个小的缩放类型图像。前面。这就是我在做的事情。

[self imageWithImage:(UIImage *)image scaledToSize:CGSizeMake(160.0f,240.0f)];

我的辩护是 -

-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();

    [self cropimage:newImage];
    return newImage;
}

在cropimage中,我这样做 -

-(void)cropimage:(UIImage *)image
{

   CGRect cropRect = CGRectMake(20,100,280,200);
     CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
    UIImage* croppedImage = [UIImage imageWithCGImage:imageRef];
         CGImageRelease(imageRef);

    NSData *pictureData = [NSData dataWithData:UIImagePNGRepresentation(croppedImage)];
}

有人能找出我的做法有什么问题吗?

1 个答案:

答案 0 :(得分:0)

尝试传递image.scale而不是0.0来强制它使用传入的图像比例。

-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();

    [self cropimage:newImage];
    return newImage;
}