2013-03-07 566 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)]];