嗨,有没有什么办法可以在iPhone SDK中将您的图像缩小变成脂肪大小反之亦然? 如何在iPhone SDK中将瘦图像更改为胖图像?
在这个应用程序中,我想让用户可以通过滑动滑块将其图像从常规大小更改为胖大小,他可以在iPhone SDK中测量其大小。 我认为这可以通过获取图像的像素来工作我已经试过这段代码来获取图像的像素,但它只是从图像中移除颜色。
UIImage *image = [UIImage imageNamed:@"foo.png"];
CGImageRef imageRef = image.CGImage;
NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
char *pixels = (char *)[data bytes];
// this is where you manipulate the individual pixels
// assumes a 4 byte pixel consisting of rgb and alpha
// for PNGs without transparency use i+=3 and remove int a
for(int i = 0; i < [data length]; i += 4)
{
int r = i;
int g = i+1;
int b = i+2;
int a = i+3;
pixels[r] = pixels[r]; // eg. remove red
pixels[g] = pixels[g];
pixels[b] = pixels[b];
pixels[a] = pixels[a];
}
// create a new image from the modified pixel data
size_t width = CGImageGetWidth(imageRef);
size_t height = CGImageGetHeight(imageRef);
size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, [data length], NULL);
CGImageRef newImageRef = CGImageCreate (
width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorspace,
bitmapInfo,
provider,
NULL,
false,
kCGRenderingIntentDefault
// the modified image
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
imgView.image = newImage;
我也尝试了从这段代码拉伸图像。
UIImage *stretchImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
有人能帮助我吗?我没有找到任何可以提供这种功能的框架或SDK。我搜索了很长时间。
我只是LOL'd!从字面上看! – WrightsCS 2012-04-17 06:32:31
+1 @WrightsCS – cfedermann 2012-04-17 06:33:39
这个问题非常好,但我在提供答案之前就像任何笑一样 – Charan 2012-04-17 06:35:13