Stitch/Composite multiple images vertically and save as one image (iOS, objective-c)

前端 未结 6 1893
一向
一向 2020-12-12 19:48

I need help writing an objective-c function that will take in an array of UIImages/PNGs, and return/save one tall image of all the images stitched together in order vertical

6条回答
  •  旧巷少年郎
    2020-12-12 20:38

    I know I'm a bit late here but hopefully this can help someone out. If you're trying to create one large image out of an array you can use this method

    - (UIImage *)mergeImagesFromArray: (NSArray *)imageArray {
    
        if ([imageArray count] == 0) return nil;
    
        UIImage *exampleImage = [imageArray firstObject];
        CGSize imageSize = exampleImage.size;
        CGSize finalSize = CGSizeMake(imageSize.width, imageSize.height * [imageArray count]);
    
        UIGraphicsBeginImageContext(finalSize);
    
        for (UIImage *image in imageArray) {
            [image drawInRect: CGRectMake(0, imageSize.height * [imageArray indexOfObject: image],
                                          imageSize.width, imageSize.height)];
        }
    
        UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return finalImage;
    }
    

提交回复
热议问题