带图像的iphone sdk按钮

带图像的iphone sdk按钮,iphone,uibutton,Iphone,Uibutton,我正试图找出如何执行以下操作: [self.activeBtn setImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal]; 我有一个按钮,有一个图像,而不是默认的外观。用户单击按钮并选择照片(使用图像选择器)。我想将照片的图像设置为用户单击的照片 因此,我做了以下工作: [self.activeBtn setImage:[info objectForKe

我正试图找出如何执行以下操作:

[self.activeBtn setImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
我有一个按钮,有一个图像,而不是默认的外观。用户单击按钮并选择照片(使用图像选择器)。我想将照片的图像设置为用户单击的照片

因此,我做了以下工作:

[self.activeBtn setImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
不幸的是,这会将图像缩放到小框中。什么是确保图像被按钮边框裁剪而不是缩放的最佳方法

编辑:要注意我当前在模拟器上。

您设置为

 [self.activeBtn setBackgroundImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];

您可以使用此函数创建一个裁剪版本,将按钮的高度和宽度作为参数传递

- (UIImage*)thumbnailOfHeight:(int)height andWidth:(int)width fromImage:(UIImage*)image{
    UIImage *thumbnail;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];                                  

    BOOL isWidthGreaterThanHeight = (image.size.width > image.size.height);
    float sideFull = (isWidthGreaterThanHeight)? image.size.height : image.size.width;

    CGRect clippedRect = CGRectMake(0, 0, sideFull, sideFull);

    UIGraphicsBeginImageContext(CGSizeMake(height, width));
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGContextClipToRect(currentContext, clippedRect);

    CGFloat scaleFactor = width/sideFull;

    if (isWidthGreaterThanHeight) {
        CGContextTranslateCTM(currentContext, -((image.size.width - sideFull)/2)*scaleFactor, 0);
    }
    else {
        CGContextTranslateCTM(currentContext,0, -((image.size.height - sideFull)/2)*scaleFactor);
    }
    CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);

    [imageView.layer renderInContext:currentContext];
    thumbnail = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    [imageView release];
    return thumbnail;
}

不,我可以先设置按钮的背景图像,这样看起来很好。当我用上面的调用设置它时,我只看到图像/按钮应该在的地方有一个空白。顺便说一下,我在模拟器上!非常感谢你的这段话!我将不得不检查这个UIGraphicsContext的东西,然后做一些图像处理的东西。嗯。。。我需要为CALayer做些什么?LLVM在“imageView.layer”上抱怨说这是CALayer的转发声明。