/**
* creates an UIImage with a similar behavior like stretchableImage, but it stretches also the
* whole content area, which is suitable for all kinds of vertical gradients
*/
- (UIImage *)imageNamed:(NSString *)name WithLeftCapWidth:(CGFloat)leftCapWidth topCapHeight:(CGFloat)topCapHeight frameRect:(CGRect) aframe{
UIImage *cropImage;
UIImage *topImage;
UIImage *bottomImage;
UIImage *basicImg = [UIImage imageNamed:name];
BOOL isScalableOS = [basicImg respondsToSelector:@selector(scale)];
CGFloat imgScale = isScalableOS?basicImg.scale:1.00f;
CGRect topRect = CGRectMake(0.0, 0.0, basicImg.size.width*imgScale, topCapHeight*imgScale);
CGRect cropRect = CGRectMake(0.0, topCapHeight*imgScale, basicImg.size.width*imgScale, basicImg.size.height*imgScale – (topCapHeight * 2*imgScale));
CGRect bottomRect = CGRectMake(0.0,basicImg.size.height*imgScale – topCapHeight*imgScale, basicImg.size.width*imgScale, topCapHeight*imgScale);
// main area
CGImageRef imageRef = CGImageCreateWithImageInRect([basicImg CGImage], cropRect);
if (isScalableOS) {
cropImage = [[UIImage imageWithCGImage:imageRef scale:imgScale orientation:basicImg.imageOrientation] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0];
}else {
cropImage = [[UIImage imageWithCGImage:imageRef] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0];
}
CGImageRelease(imageRef);
// top area
CGImageRef imageRef2 = CGImageCreateWithImageInRect([basicImg CGImage], topRect);
if (isScalableOS) {
topImage = [[UIImage imageWithCGImage:imageRef2 scale:imgScale orientation:basicImg.imageOrientation] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0];
} else {
topImage = [[UIImage imageWithCGImage:imageRef2] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0];
}
CGImageRelease(imageRef2);
// bottom area
CGImageRef imageRef3 = CGImageCreateWithImageInRect([basicImg CGImage], bottomRect);
if (isScalableOS) {
bottomImage = [[UIImage imageWithCGImage:imageRef3 scale:imgScale orientation:basicImg.imageOrientation] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0];
} else {
bottomImage = [[UIImage imageWithCGImage:imageRef3] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0];
}
CGImageRelease(imageRef3);
if (UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(aframe.size, NO, imgScale);
}else {
UIGraphicsBeginImageContext(aframe.size);
}
[topImage drawInRect:CGRectMake(0.0, 0.0, aframe.size.width, topCapHeight)];
[cropImage drawInRect:CGRectMake(0.0, topCapHeight, aframe.size.width, aframe.size.height - (topCapHeight*2))];
[bottomImage drawInRect:CGRectMake(0.0, aframe.size.height - topCapHeight, aframe.size.width, topCapHeight)];
basicImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return basicImg;
}
Noch keine Kommentare vorhanden.