如何仅重复图像的一部分?
如何仅重复图像的一部分,
有两个图像。
https://i.sstatic.net/KC0Pm.png
源图片类似于上面的内容 我想将其更改为:
https://i.sstatic.net/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;
:
是这样的 https://i.sstatic.net/jO0O5.png
有什么问题吗?
谢谢
how to repeat only part of an image,
there are two images.
https://i.sstatic.net/KC0Pm.png
the source pic is something like the above
and i want to change it to:
https://i.sstatic.net/t8CwQ.png
this means i just want to repeat only part of the image? not stretch the source pic,
is it possible to do that?
thanks
Update:
CODE HERE : http://www.box.net/shared/cmtz0e48a5urtajxu4yn
still have problem!!
Update:
i crop my image file and just repeat the part,but i still have some problem.
- (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;
}
the result is like this:
https://i.sstatic.net/jO0O5.png
is there something wrong?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iOS 5 中有
stretchableImageWithLeftCapWidth:topCapHeight:
(最高 4.3)或ressizedImageWithCapInsets:
There's
stretchableImageWithLeftCapWidth:topCapHeight:
(up to 4.3) orresizableImageWithCapInsets:
in iOS 5使用 UIImage,您只能重复图像的 1 像素宽部分。您能做的最好的事情就是裁剪图像文件并重复您想要重复的部分。您也可以使用 Core Graphics 来完成此操作,但它比修改文件要复杂得多。
With UIImage you can only repeat a 1 pixel wide part of the image. The best thing you can do is to crop your image file and just repeat the part you want to repeat. You can do it with Core Graphics too, but it's much more complicated than modifying your file.
您可以使用
NSDrawThreePartImage
(仅限 OSX)来提示一种可能的解决方案。它的声明如下:
因此,您将需要三个图像来模拟它。
如果您还知道偏移量,您实际上可以使用一张图像和一些奇特的 CoreGraphics 操作来完成此操作。
模拟时首先想到的是使用 CGBitmapContext ,然后使用上下文将图像绘制到位图上,然后将结果作为位图或
>CGImage
(您可能想要保存结果,因为计算成本可能很高)。You can use
NSDrawThreePartImage
(OSX only) for a hint at one potential solution.It is declared like so:
So you will need three images to simulate this.
You can actually do this with one image and some fancy CoreGraphics manipulations if you also know the offsets.
The first thing that comes to mind when simulating this would be to use a
CGBitmapContext
, then just draw the images where you want them to the bitmap using the context, then take the result as a bitmap orCGImage
(you'll probably want to save the result, because it can be expensive to compute).