【发布时间】:2016-04-10 07:31:40
【问题描述】:
【问题讨论】:
-
本教程可能对raywenderlich.com/69855/…有帮助
-
用Photoshop怎么样?
标签: ios objective-c core-image
【问题讨论】:
标签: ios objective-c core-image
UIImage *image1 = [UIImage imageNamed:@"image1.jpg"];
UIImage *image2 = [UIImage imageNamed:@"image2.jpg"];
CGSize newSize = image1.size; //set new size as you want
UIGraphicsBeginImageContext( newSize );
//crop image1
CGImageRef imageRef = CGImageCreateWithImageInRect([image1 CGImage], CGRectMake(0, 0, image1.size.width/2, image1.size.height));
image1 = [UIImage imageWithCGImage:imageRef scale:image1.scale orientation:image1.imageOrientation];
CGImageRelease(imageRef);
//crop image2
imageRef = CGImageCreateWithImageInRect([image2 CGImage], CGRectMake(0, 0, image2.size.width/2, image2.size.height));
image2 = [UIImage imageWithCGImage:imageRef scale:image2.scale orientation:image2.imageOrientation];
CGImageRelease(imageRef);
//combine both images
// Use existing opacity as is
[image1 drawInRect:CGRectMake(0, 0, newSize.width/2, newSize.height)];
// Apply supplied opacity if applicable
[image2 drawInRect:CGRectMake(newSize.width/2, 0, newSize.width/2, newSize.height) blendMode:kCGBlendModeNormal alpha:1];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *strPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) objectAtIndex:0];
strPath = [strPath stringByAppendingPathComponent:@"img.png"];
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];
NSData *imgdata = UIImagePNGRepresentation(newImage);
[imgdata writeToFile:strPath atomically:false];
NSLog(@"Path = %@",strPath); //on this path image will be stored
【讨论】:
更简单的是,您可以使用 CISwipeTransition 并将 inputTime 设置为 0.5,
西蒙
【讨论】:
inputImage 和 inputTargetImage。它主要用于在幻灯片放映等应用程序中的图像之间转换,但通过将inputTime 设置为 0.5,它可以提供两个图像的分屏视图。恐怕我不会说 ObjC,但我确实有一个用于探索 CI 转换的 Swift 演示应用程序:github.com/FlexMonkey/CoreImageTransitionExplorer
如果你想使用 Core Graphics,你可以创建一个渐变蒙版,绘制第一张图像,应用渐变作为剪贴蒙版,然后绘制第二张图像。因此,第一张图像未被剪裁,第二张图像被剪裁为渐变:
- (UIImage *)imageCombiningImage:(UIImage *)image1 withImage:(UIImage *)image2 {
CGRect rect = CGRectMake(0, 0, image1.size.width, image1.size.height);
UIGraphicsBeginImageContextWithOptions(image1.size, TRUE, image1.scale);
// create gradient
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.45, 0.55 }; // gradient goes from 45% to 55% the way across the image
CGFloat components[] = {
1.0, 1.0, 1.0, 1.0, // Start color ... white
0.0, 0.0, 0.0, 0.0 // End color ... clear
};
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);
// create mask from that gradient
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(image1.size.width, 0), kCGGradientDrawsAfterEndLocation);
CGImageRef gradientImageRef = CGBitmapContextCreateImage(context);
// draw the first image
[image1 drawInRect:rect];
// clip subsequent drawing to the gradient mask we just created
CGContextClipToMask(context, rect, gradientImageRef);
// draw the second image
[image2 drawInRect:rect];
// extract the image
UIImage *combinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// clean up
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
CGImageRelease(gradientImageRef);
return combinedImage;
}
结果:
【讨论】:
_View1.frame = CGRectMake(0, 0, Width/2, Width);
_View2.frame = CGRectMake(0, 0, Width/2, Width);
CAGradientLayer *gradientMask = [CAGradientLayer layer];
gradientMask.frame = self.View2.bounds;
gradientMask.colors = @[(id)[UIColor whiteColor].CGColor,
(id)[UIColor clearColor].CGColor];
gradientMask.startPoint = CGPointMake(0.5, 0.5); // start at left middle
gradientMask.endPoint = CGPointMake(0, 0.5); // end at right middle
self.View2.layer.mask = gradientMask;
【讨论】: