M
M
Max Onflic2016-01-13 20:38:28
Objective-C
Max Onflic, 2016-01-13 20:38:28

How to merge UIImage's quickly?

+ (UIImage *)imageByCombiningImage:(UIImage*)firstImage withImage:(UIImage*)secondImage {
    UIImage *image = nil;
    
    CGSize newImageSize = CGSizeMake(MAX(firstImage.size.width, secondImage.size.width), MAX(firstImage.size.height, secondImage.size.height));
    if (UIGraphicsBeginImageContextWithOptions != NULL) {
        UIGraphicsBeginImageContextWithOptions(newImageSize, NO, [[UIScreen mainScreen] scale]);
    } else {
        UIGraphicsBeginImageContext(newImageSize);
    }
    [firstImage drawAtPoint:CGPointMake(roundf((newImageSize.width-firstImage.size.width)/2),
                                        roundf((newImageSize.height-firstImage.size.height)/2))];
    [secondImage drawAtPoint:CGPointMake(roundf((newImageSize.width-secondImage.size.width)/2),
                                         roundf((newImageSize.height-secondImage.size.height)/2))];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

Runs too long on the main thread. What is the best way to parallelize and is it possible?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexey Bukhtin, 2016-01-21
@bubuh

// Переходим в рабочий поток.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // Выполняем мерджинг.
    UIImage *resultImage = [UIImage imageByCombiningImage:firstImage withImage:secondImage];
    // Возвращаем картинку в главный поток.
    dispatch_sync(dispatch_get_main_queue(), ^{
        imageView.image = resultImage;
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question