2013-03-07 565 views
0

UIButton,如何设置背景图片的大小和位置?如何设置UIButton图片的大小和位置

例如,如何改变大小和图像位置,以便在UIButton

编辑的顶部:使用setImageEdgeInsets:固定的问题,如:

[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -image.size.width, 0, 0)]; 
[button setImageEdgeInsets:UIEdgeInsetsMake(0, button.titleLabel.bounds.size.width, 0, 0)]; 
+0

你想修改什么? – nsgulliver 2013-03-07 17:00:43

+0

我相信你正在寻找UIButton的图像属性'[myButton setImage:<#(UIImage *)#> forState:<#(UIControlState)#>];' – 2013-03-07 17:00:53

+0

我想修改图像背景的大小而不调整按钮大小框架,如果我添加一个子视图,我不会有动画的触摸 – james075 2013-03-07 17:04:45

回答

1

AFAIK有像没有财产,但在我的意见你将不得不根据UIButton的大小来缩放图像。

例如,你可以使用此代码

UIImage *resultImage = [self imageByScalingAndCroppingForSize:CGSizeMake(button.frame.size.height,button.frame.size.width) : sourchImage]; 

方法缩放

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize:(UIImage *)srcimage 
{ 
    UIImage *sourceImage = srcimage; 
    UIImage *newImage = nil;   
    CGSize imageSize = sourceImage.size; 
    CGFloat width = imageSize.width; 
    CGFloat height = imageSize.height; 
    CGFloat targetWidth = targetSize.width; 
    CGFloat targetHeight = targetSize.height; 
    CGFloat scaleFactor = 0.0; 
    CGFloat scaledWidth = targetWidth; 
    CGFloat scaledHeight = targetHeight; 
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
    { 
     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor > heightFactor) 
      scaleFactor = widthFactor; // scale to fit height 
     else 
      scaleFactor = heightFactor; // scale to fit width 
     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // center the image 
     if (widthFactor > heightFactor) 
     { 
      thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
     else 
      if (widthFactor < heightFactor) 
      { 
       thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
      } 
    }  

    UIGraphicsBeginImageContext(targetSize); // this will crop 

    CGRect thumbnailRect = CGRectZero; 
    thumbnailRect.origin = thumbnailPoint; 
    thumbnailRect.size.width = scaledWidth; 
    thumbnailRect.size.height = scaledHeight; 

    [sourceImage drawInRect:thumbnailRect]; 

    newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    if(newImage == nil) 
     NSLog(@"could not scale image"); 

    //pop the context to get back to the default 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
1

如果你真的想缩放图像,做到这一点,但使用它之前,你应该调整它。在运行时调整它的大小就会丢失CPU周期。

这是我使用缩放图像的类别:

的UIImage + Extra.h

@interface UIImage (Extras) 
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize; 
@end; 

的UIImage + Extra.m

@implementation UIImage (Extras) 

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize { 

UIImage *sourceImage = self; 
UIImage *newImage = nil; 

CGSize imageSize = sourceImage.size; 
CGFloat width = imageSize.width; 
CGFloat height = imageSize.height; 

CGFloat targetWidth = targetSize.width; 
CGFloat targetHeight = targetSize.height; 

CGFloat scaleFactor = 0.0; 
CGFloat scaledWidth = targetWidth; 
CGFloat scaledHeight = targetHeight; 

CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

if (!CGSizeEqualToSize(imageSize, targetSize)) { 

     CGFloat widthFactor = targetWidth/width; 
     CGFloat heightFactor = targetHeight/height; 

     if (widthFactor < heightFactor) 
       scaleFactor = widthFactor; 
     else 
       scaleFactor = heightFactor; 

     scaledWidth = width * scaleFactor; 
     scaledHeight = height * scaleFactor; 

     // center the image 

     if (widthFactor < heightFactor) { 
       thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } else if (widthFactor > heightFactor) { 
       thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
     } 
} 


// this is actually the interesting part: 

UIGraphicsBeginImageContext(targetSize); 

CGRect thumbnailRect = CGRectZero; 
thumbnailRect.origin = thumbnailPoint; 
thumbnailRect.size.width = scaledWidth; 
thumbnailRect.size.height = scaledHeight; 

[sourceImage drawInRect:thumbnailRect]; 

newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

if(newImage == nil) NSLog(@"could not scale image"); 


return newImage ; 
} 

@end 

你可以用它来你想要的大小。像:

[self.itemImageButton setImage:[stretchImage imageByScalingProportionallyToSize:CGSizeMake(20,20)]];