UIView背景图像按比例调整大小

Kai*_*bin 1 iphone cocoa-touch objective-c uiview ios

我有一个大小为200*100的Background.png图像,我为uiview设置了背景图像:

    UIView view= UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Background.png"]];
Run Code Online (Sandbox Code Playgroud)

现在,我通过以下方式重置框架UIView:

    [view setFrame:CGRectMake(50,100,400,200)];
Run Code Online (Sandbox Code Playgroud)

我需要按照uiview的比例调整背景图像的大小,但我只有一个固定大小的Background.png图像.我该怎么办.

Div*_*ert 6

相应地考虑你的尺寸

    UIGraphicsBeginImageContext( newSize );
   [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
   UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

    UIView view= UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
    view.backgroundColor = [[UIColor alloc] initWithPatternImage:newImage];
Run Code Online (Sandbox Code Playgroud)

否则采取imageview并设置contentMode,如果你不想平铺图像