Merge Two Image on to one Image programmatically in iphone

前端 未结 10 829
旧巷少年郎
旧巷少年郎 2020-12-04 17:01

Hello I am developing the application which requires to merge two Image , My image size are 320*240 by merging both Image I want the size to be 320 * 480 . How can i do this

10条回答
  •  半阙折子戏
    2020-12-04 17:25

    To avoid degrading image quality, use UIGraphicsBeginImageContextWithOptions.

    If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen.

    UIImage *image1 = [UIImage imageNamed:@"image1.png"];
    UIImage *image2 = [UIImage imageNamed:@"image2.png"];
    
    CGSize size = CGSizeMake(image1.size.width, image1.size.height + image2.size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, false, 0.0) // Use this call
    
    [image1 drawInRect:CGRectMake(0,0,size.width, image1.size.height)];
    [image2 drawInRect:CGRectMake(0,image1.size.height,size.width, image2.size.height)];
    
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    //Add image to view
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, finalImage.size.width, finalImage.size.height)];
    imageView.image = finalImage;
    [self.view addSubview:imageView];
    

提交回复
热议问题