Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

core graphics - How use CGImageCreateWithImageInRect for iPhone 4 (HD)?

I use the following code to getting images from the sprite. And it works fine everywhere except the iPhone 4 (HD version).

- (UIImage *)croppedImage:(CGRect)rect {
    CGImageRef image = CGImageCreateWithImageInRect([self CGImage], rect);
    UIImage *result = [UIImage imageWithCGImage:image];
    CGImageRelease(image);
    return result;
}

The iPhone 4 automatically load HD version of the image ([email protected]) instead sprite.png. The original image has a scale 2, but the resulting image has a scale 1 and wrong size.

How to handle this behavior taking into account the different scales for iPhone 3G[s] and the iPhone 4?

I have read this document, but about the use CGImageCreateWithImageInRect here says nothing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From what I can tell the CGImageCreateWithImageInRect will do the right thing. What you need to change is the UIImage initilization

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageWithCGImage:scale:orientation:

Change it to [UIImage imageWithCGImage:image scale:self.scale orientation:self. imageOrientation] and it should work just fine. (this is assuming this is a category on UIImage which it looks like it is)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...