2011-10-26 62 views
3

如何重複只有一部分圖像,如何僅重複一部分圖像?

有兩個圖像。

i.stack.imgur.com/KC0Pm.png

源PIC是像上述 ,我想將其更改爲:

i.stack.imgur.com /t8CwQ.png

這意味着我只想重複部分圖像?不拉伸源圖片, 是否有可能做到這一點?

感謝 更新:

代碼在這裏:http://www.box.net/shared/cmtz0e48a5urtajxu4yn 還是有問題!

更新: 我裁剪我的圖像文件,只是重複該部分,但我仍然有一些問題。

- (UIImage *)imageWithImage:(NSString *)iconName covertToSize:(CGSize)size { 
CGRect topLeft,topRight,bottomLeft,bottomRight,top,bottom,left,right; 
if ([iconName isEqualToString:@"border_baikuang"]) { 
    topLeft = CGRectMake(0, 0, 18, 18); 
    topRight = CGRectMake(108-18, 0, 18, 18); 
    bottomLeft = CGRectMake(0, 104-18, 18, 18); 
    bottomRight = CGRectMake(108-18, 104-18, 18, 18); 
    top = CGRectMake(18, 0, 1, 18); 
    bottom = CGRectMake(18, 104-18, 1, 18); 
    left = CGRectMake(0, 18, 18, 1); 
    right = CGRectMake(108-18, 18, 18, 1); 
} else if ([iconName isEqualToString:@"border_fuguxiangkuang"]) { 
    topLeft = CGRectMake(0, 0, 28, 28); 
    topRight = CGRectMake(108-28, 0, 28, 28); 
    bottomLeft = CGRectMake(0, 104-28, 28, 28); 
    bottomRight = CGRectMake(108-28, 104-28, 28, 28); 
    top = CGRectMake(28, 0, 28, 24); 
    bottom = CGRectMake(28, 104-28, 28, 24); 
    left = CGRectMake(0, 28, 24, 28); 
    right = CGRectMake(108-24, 28, 24, 28); 
} 
float width = size.width; 
float height = size.height; 
UIGraphicsBeginImageContext(size); 
// 
UIImage *topLeftPattern = [self fetchImgWithSrc:iconName rect:topLeft]; 
[topLeftPattern drawInRect:CGRectMake(0, 0, topLeftPattern.size.width, topLeftPattern.size.height)]; 

UIImage *topRightPattern = [self fetchImgWithSrc:iconName rect:topRight]; 
[topRightPattern drawInRect:CGRectMake(width-topRightPattern.size.width, 0, topRightPattern.size.width, topRightPattern.size.height)]; 

UIImage *bottomLeftPattern = [self fetchImgWithSrc:iconName rect:bottomLeft]; 
[bottomLeftPattern drawInRect:CGRectMake(0, height-bottomLeftPattern.size.height, bottomLeftPattern.size.width, bottomLeftPattern.size.height)]; 

UIImage *bottomRightPattern = [self fetchImgWithSrc:iconName rect:bottomRight]; 
[bottomRightPattern drawInRect:CGRectMake(width-bottomRightPattern.size.width, height-bottomRightPattern.size.height, bottomRightPattern.size.width, bottomRightPattern.size.height)]; 

UIImage *topPattern = [self fetchImgWithSrc:iconName rect:top]; 
[topPattern drawAsPatternInRect:CGRectMake(topLeftPattern.size.width, 0, width-topRightPattern.size.width-topLeftPattern.size.width, topPattern.size.height)]; 

UIImage *bottomPattern = [self fetchImgWithSrc:iconName rect:bottom]; 
[bottomPattern drawAsPatternInRect:CGRectMake(bottomLeftPattern.size.width, height-bottomLeftPattern.size.height, width-bottomRightPattern.size.width-bottomLeftPattern.size.width, bottomPattern.size.height)]; 

UIImage *leftPattern = [self fetchImgWithSrc:iconName rect:left]; 
[leftPattern drawAsPatternInRect:CGRectMake(0, topLeftPattern.size.height, leftPattern.size.width, height-bottomLeftPattern.size.height-topLeftPattern.size.height)]; 

UIImage *rightPattern = [self fetchImgWithSrc:iconName rect:right]; 
[rightPattern drawAsPatternInRect:CGRectMake(width-topRightPattern.size.width, topRightPattern.size.height, rightPattern.size.width, height-bottomRightPattern.size.height - topRightPattern.size.height)]; 

UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext(); 
return destImage; 

}

- (UIImage*)fetchImgWithSrc:(NSString *)iconName rect:(CGRect)clipRect { 

NSString *completeName = [iconName stringByAppendingString:@".png"]; 
UIImage *src = [UIImage imageFromFileName:completeName]; 
NSLog(@"src=%@",NSStringFromCGSize(src.size)); 
UIImage *tmpPattern = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(src.CGImage, clipRect)]; 
NSLog(@"dest=%@",NSStringFromCGSize(tmpPattern.size)); 
return tmpPattern; 

}

的結果是這樣的: http://i.stack.imgur.com/jO0O5.png

是有什麼錯嗎?

感謝

回答

1

隨着UIImage的,你只能重複圖像的1個像素寬的部分。你可以做的最好的事情是裁剪你的圖像文件,並重復你想重複的部分。你也可以用Core Graphics來完成它,但它比修改你的文件要複雜得多。

+0

感謝您的幫助 – guoxj

+0

仍然有問題。 – guoxj

0

您可以使用NSDrawThreePartImage(僅限OSX)提示一種潛在的解決方案。

它宣稱,像這樣:

void NSDrawThreePartImage(NSRect frame, 
    NSImage *startCap, 
    NSImage *centerFill, 
    NSImage *endCap, 
    BOOL vertical, 
    NSCompositingOperation op, 
    CGFloat alphaFraction, 
    BOOL flipped 
); 

所以,你需要三個圖像來模擬這一點。

如果你也知道偏移量,你可以用一個圖像和一些花哨的CoreGraphics操作實際做到這一點。

模擬這個時首先想到的是使用CGBitmapContext,然後使用上下文將圖像繪製到位圖,然後將結果作爲位圖或CGImage(您會可能想要保存結果,因爲計算起來可能很昂貴)。

5

stretchableImageWithLeftCapWidth:topCapHeight:(高達4.3)或resizableImageWithCapInsets: iOS 5中

相關問題