Iphone UIGraphicsBeginImageContextWithOptions和UIImageJPEG表示不能很好地协同工作

Iphone UIGraphicsBeginImageContextWithOptions和UIImageJPEG表示不能很好地协同工作,iphone,objective-c,uiimagejpegrepresentation,Iphone,Objective C,Uiimagejpegrepresentation,因此,我有以下代码来创建UIImage: UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0); [border.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

因此,我有以下代码来创建UIImage:

UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);
[border.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
此时,图像上的大小正确,为80x100

然后运行以下代码:

NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f);
图像的NSData返回一个大小为160x200的图像,是其应有大小的两倍

很明显,这是因为:

UIGraphicsBeginImageContextWithOptions(border.frame.size,是,0)


末端的0是比例,因为它是0,所以按设备比例因子。我这样做是为了保持清晰的形象。然而,当我将图像设置为1时,尽管图像保持了应有的大小,但它的视网膜质量却不高。我想做的是保持它的视网膜质量,但也要保持它在正确的大小。有没有办法做到这一点?

在调用UIImageJPEGRepresentation之前,请尝试调整UIImage的大小

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);  
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();    

    return newImage;
}

if([UIScreen mainScreen].scale > 1)
    {
        thumbnailImage = [self thumbnailImage newSize:CGSizeMake(thumbnailImage.size.width/[UIScreen       mainScreen].scale, thumbnailImage.size.height/[UIScreen mainScreen].scale)];
    }
- (UIImage *)imageWithImage{
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;

}