UIBarButtonItem设定图片

设置背景图片

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageWithData:data] style:UIBarButtonItemStyleDone target:self action:nil];
效果:

UIBarButtonItem设定图片_第1张图片

修改图片模式后

UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageWithData:data]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:nil];
UIBarButtonItem设定图片_第2张图片

修正图片的大小

- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return [reSizeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}


UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[self reSizeImage:[UIImage imageWithData:data] toSize:CGSizeMake(30, 30)] style:UIBarButtonItemStyleDone target:self action:nil];


UIBarButtonItem设定图片_第3张图片

一切正常了


你可能感兴趣的:(IOS)