2011-08-07 4 views
3

を働いていないので、私はUIImageを作成するには、このコードを持っている:UIGraphicsBeginImageContextWithOptionsとUIImageJPEGRepresentationよく一緒に

UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0); 
[border.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *thumbnailImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

この時点では、画像のサイズは、80x100正しいです。

それから、このコードを実行します:

NSData *fullImageData = UIImageJPEGRepresentation(image, 1.0f); 

と画像のNSDataのは、サイズ160x200の画像を返す - それがあるべきの2倍。

それは、その理由が明らかになったのLINEさ:

UIGraphicsBeginImageContextWithOptions(border.frame.size, YES, 0);

0最後には規模であり、それは0ですので、それはデバイスのスケールファクタで行きます。私はそれを明確なイメージを維持するためにこのように保ちます。しかし、私は1に画像を設定すると、それはすべきサイズにイメージにもかかわらず、それは網膜の品質で出ていません。私がやりたいことは、網膜の品質でそれを保つだけでなく、それを正しいサイズに保つことです。これを行う方法はありますか?

答えて

2

- (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)]; 
    } 
0
- (UIImage *)imageWithImage{ 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 

} 
UIImageJPEGRepresentationを呼び出す前にUIImageのサイズを変更してください