我想創建一個看起來好像2個圖像互相交織的效果。所以我縮放了第一張圖片作爲背景,並且我在背景上使用了幾個UIIMageView來循環播放第二張圖片,以提供我想要的外觀。這適用於我使用不需要縮放以適合顯示尺寸的圖像,但是當我使用UIGraphics調整圖像大小時,出現圖像質量下降的問題。任何建議將不勝感激。如何使用CGImageRef來循環調整大小而不增加像素的圖像部分?
圖片1.JPG爲2112×2816
圖片2.JPG是2000×3008
UIImage *bgImage = [UIImage imageNamed:@"1.jpg"];
UIImageView *bgView = [[UIImageView alloc] initWithImage:bgImage];
bgView.contentMode = UIViewContentModeScaleToFill;
bgView.frame = CGRectMake(0,0,CGRectGetWidth(self.view.frame), (CGRectGetWidth(self.view.frame)/bgImage.size.width) * bgImage.size.height);
bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:bgView];
UIImage *frontImage = [UIImage imageNamed:@"2.jpg"];
CGSize newSize = CGSizeMake(CGRectGetWidth(self.view.frame), (CGRectGetWidth(self.view.frame)/frontImage.size.width) * frontImage.size.height);
UIGraphicsBeginImageContextWithOptions(newSize, YES, 0.0);
[frontImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
int count = 0;
int varX = 10;
int varY = 10;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
CGFloat tileWidth = width/varX;
CGFloat tileHeight = height/varY;
for (int x = 0; x < varX; x++) {
for (int y = 0; y < varY; y++) {
NSLog(@"%f, %f, %f, %f",x*tileWidth, y*tileHeight, tileWidth, tileHeight);
CGImageRef scaledRef = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(x*tileWidth, y*tileHeight, tileWidth, tileHeight));
UIImage *image = [UIImage imageWithCGImage:scaledRef scale:scaledImage.scale orientation:scaledImage.imageOrientation];
UIImageView *bgx = [[UIImageView alloc] initWithImage:image];
CGImageRelease(scaledRef);
bgx.frame = CGRectMake((x*tileWidth), (y*tileHeight), tileWidth, tileHeight);
bgx.backgroundColor = [UIColor yellowColor];
bgx.contentMode = UIViewContentModeTopLeft;
if(count%2==0){
[self.view addSubview:bgx];
}
count++;
}
count++;
}
目前尚不清楚您是向上還是向下放大。放大將總會產生更差的圖像質量。對於一般縮放,我多年來一直使用這個UIImage類別:http://www.catamount.com/forums/viewtopic.php?f=21&t=967&sid=b1096cb20757776f3259c1bd8f7db7ee – 2014-10-01 10:46:28