开发者

How to set a image as the background of UIView but scaled properly?

I would like the user to be able to select a image from their photos and set it to the background of the main view.

This is what I'm currently doing:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];

    UIImage *testimg = [info objectForKey:UIImagePickerControllerOriginalImage];

    NSArray  * sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );

    filePath = [[NSString alloc] initWithFormat: @"%@/%@", [sysPaths objectAtIndex: 0], @"testimg.png"];
    if ( [UIImagePNGRepresentation ( testimg) writeToFile:filePath atomically:YES] )    ;

    self.backgroundColor= [[UIColor alloc] initWithPatternImage:[[UIImage alloc] initWithContentsOfFile: filePath]];


     }

开发者_如何学GoAside from this method being a bit sluggish in speed, this works great! There is one problem though. When the image is selected the image is not scaled properly. How would I get around this?

Thanks in advance,

-David


Have a look at UIImageView in particular the contentMode property.

imageView.contentMode = UIViewContentModeScaleAspectFit;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜