这几年项目开发中的小程序片段整理

2017-11-22  本文已影响0人  烟雨平生花飞舞

1、cell的几种创建方式:

2、判断webview是否加载完成:

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

//在此处改变headerview的高度

// js获取高度

self.scrollHeight = [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"] floatValue];

if (!webView.isLoading) {

NSString *readyState = [webView stringByEvaluatingJavaScriptFromString:@"document.readyState"];

BOOL complete = [readyState isEqualToString:@"complete"];

if (complete) {

self.isLoadFinish = YES;

}

}

}

3、webview滚动截图:

- (UIImage *)screenShotWithScrollView:(UIScrollView *)scrollView

{

UIImage* image;

UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, [UIScreen mainScreen].scale);

{

CGPoint savedContentOffset = scrollView.contentOffset;

CGRect savedFrame = scrollView.frame;

scrollView.contentOffset = CGPointZero;

scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);

[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];

image = UIGraphicsGetImageFromCurrentImageContext();

scrollView.contentOffset = savedContentOffset;

scrollView.frame = savedFrame;

}

UIGraphicsEndImageContext();

NSData *data = UIImageJPEGRepresentation(image, [UIScreen mainScreen].scale);

[data writeToFile:[self getPathWithDocument:@"protocol" fileName:@"protocol.png"] atomically:NO];

if (image != nil)

{

return image;

}

return nil;

}

- (NSString *)getPathWithDocument:(NSString *)document fileName:(NSString *)name

{

NSString *path = [NSString stringWithFormat:@"Documents/%@",document];

NSString *fileName = [NSString stringWithFormat:@"%@",name];

path = [NSString getFilePathWithDir:path fileName:fileName];

NSLog(@"path == %@",path);

return path;

}

+ (NSString *)getFilePath:(NSString *)aPath

{

return [NSHomeDirectory() stringByAppendingPathComponent:aPath];

}

+ (NSString *)getFilePathWithDir:(NSString *)aDir fileName:(NSString *)aName

{

NSString *path = [self getFilePath:aDir];

NSFileManager *manager = [NSFileManager defaultManager];

if (![manager fileExistsAtPath:path]) {

[manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];

}

return [path stringByAppendingPathComponent:aName];

}

4、图片合成:

//获得某个范围内的屏幕图像

- (UIImage *)imageFromView:(UIView *)theView atFrame:(CGRect)r

{

UIGraphicsBeginImageContext(CGSizeMake(theView.width, _signatureView.bottom));

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSaveGState(context);

UIRectClip(r);

[theView.layer renderInContext:context];

UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return  theImage;

}

- (UIImage *)combine:(UIImage *)topImage bottomImage:(UIImage *)bottomImg imageSacle:(CGFloat)scale

{

CGFloat height = topImage.size.height + bottomImg.size.height;

CGSize offScreenSize = CGSizeMake(self.view.width, height);

UIGraphicsBeginImageContextWithOptions(offScreenSize, YES, scale);

CGRect rect = CGRectMake(0, 0, self.view.width, topImage.size.height);

[topImage drawInRect:rect];

rect.origin.y+=topImage.size.height;

CGRect rect1 = CGRectMake(0, rect.origin.y, self.view.width, bottomImg.size.height);

[bottomImg drawInRect:rect1];

UIImage *imagez = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

NSData *data = UIImageJPEGRepresentation(imagez, 0.5);

[data writeToFile:[self getPathWithDocument:@"protocol" fileName:@"protocolSignature.png"] atomically:NO];

return imagez;

}

5、

上一篇 下一篇

猜你喜欢

热点阅读