在我的项目中,我有'更改徽标'选项。 单击“更改徽标”前面的此灰色徽标时,将打开图库,用户可从中选择一张照片。这是使用以下代码完成的:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = info[UIImagePickerControllerOriginalImage];
[imagePicker dismissViewControllerAnimated:YES completion:nil];
imgToBeSaved = image;
self.imgViewChooseLogo.contentMode = UIViewContentModeScaleAspectFit;
self.imgViewChooseLogo.image = image;
imageChanged = YES;
}
现在此图像保存在文档目录中。因为此图像应作为所有视图控制器的徽标。所以我使用以下代码将其设置为左栏按钮项:
NSData *imageData = [NSData dataWithContentsOfFile:[AppDelegate documentDirectoryWithFilePath]];
UIImage *image = [UIImage imageWithData:imageData];
UIImage *new = [AppDelegate imageWithImage:image scaledToSize:CGSizeMake(150, 40)];
UIImage *new1 = [new imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:new1 style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
其中方法scaledToSize为
+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
我使用了150 * 40,因此我的默认徽标非常适合。当我必须使用从库中选择的其他图像时出现问题。它延伸到条形按钮项目。我不知道如何设置选择的图库图像的高度和宽度的比例,以便图像看起来不会在左栏按钮项目上看到。