分享图片剪裁

2017-03-27  本文已影响24人  又又轻

- (UIImage *)snapshotView {

UIGraphicsBeginImageContextWithOptions(CGSizeMake(FBScreenW, FBScreenH - FBNavigationBarH), NO, [UIScreen mainScreen].scale);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(currentContext, 0, 0);

[self.viewController.view.layer renderInContext:currentContext];

UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageView *snapshotView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FBScreenW, FBScreenH - FBNavigationBarH)];

UIEdgeInsets snapshotEdgeInsets = UIEdgeInsetsZero;

snapshotView.image = [snapshotImage resizableImageWithCapInsets:snapshotEdgeInsets];

return snapshotView.image;

}

- (void)composeImg {

UIImage *img = [UIImage imageNamed:@"image_forUse_image1"];

CGImageRef imgRef = img.CGImage;

CGFloat w = FBScreenW;

CGFloat h = w / 2;

//以1.png的图大小为底图

UIImage *img1 = [self snapshotView];

CGImageRef imgRef1 = img1.CGImage;

CGFloat w1 = FBScreenW;

CGFloat h1 = FBScreenH;

//以1.png的图大小为画布创建上下文

UIGraphicsBeginImageContext(CGSizeMake(w1, h1));

[img1 drawInRect:CGRectMake(0, 0, w1, h1 - FBNavigationBarH)];//先把1.png画到上下文中

[img drawInRect:CGRectMake(0, h1 - h, w, h)];//再把小图放在上下文中

UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片

UIGraphicsEndImageContext();//关闭上下文

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSString *filePath = [path stringByAppendingPathComponent:@"01.jpg"];

[UIImagePNGRepresentation(resultImg) writeToFile:filePath atomically:YES];//保存图片到沙盒

NSLog(@"%@", FBDocument);

CGImageRelease(imgRef);

CGImageRelease(imgRef1);

}

上一篇 下一篇

猜你喜欢

热点阅读