• 代码片段


    1.当前View画成Image

    - (UIImage *) imageWithUIView:(UIView*) view
    {    
    UIGraphicsBeginImageContext(view.bounds.size); 
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:ctx];
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return tImage;
    } 
    2.图片压缩
    用法: UIImage *yourImage= [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];
    // 压缩图片
    - ( UIImage *)imageWithImageSimple:( UIImage *)image scaledToSize:( CGSize )newSize
    {
    // Create a graphics image context
    UIGraphicsBeginImageContext (newSize);
    // Tell the old image to draw in this newcontext, with the desired
    // new size
    [image  drawInRect : CGRectMake ( 0 , 0 ,newSize. width ,newSize. height )];
    // Get the new image from the context
    UIImage * newImage =  UIGraphicsGetImageFromCurrentImageContext ();
    // End the context
    UIGraphicsEndImageContext ();
    // Return the new image.
    return  newImage;
    }
    3.图片上传代码
    - ( IBAction )uploadButton:( id )sender {
    UIImage  *image = [ UIImage   imageNamed : @"1.jpg" ]; // 图片名
    NSData  *imageData =  UIImageJPEGRepresentation (image, 0.5 );// 压缩比例
    NSLog ( @" 字节数 :%i" ,[imageData length]);
    // post url
    NSString  *urlString =  @"http://192.168.1.113:8090/text/UploadServlet" ;
    // 服务器地址
    // setting up the request object now
    NSMutableURLRequest  *request = [[ NSMutableURLRequest   alloc ]  init ] ;
    [request  setURL :[ NSURL   URLWithString :urlString]];
    [request  setHTTPMethod : @"POST" ];
    //
    NSString  *boundary = [ NSString   stringWithString : @"---------------------------14737809831466499882746641449" ];
    NSString  *contentType = [ NSString   stringWithFormat : @"multipart/form-data;boundary=%@",boundary];
    [request  addValue :contentType  forHTTPHeaderField :  @"Content-Type" ];
    //
    NSMutableData  *body = [ NSMutableData   data ];
    [body  appendData :[[ NSString   stringWithFormat : @"
    --%@
    " ,boundary] dataUsingEncoding : NSUTF8StringEncoding ]];
    [body  appendData :[[ NSString   stringWithString : @"Content-Disposition:form-data; name="userfile"; filename="2.png"
    " ]  dataUsingEncoding : NSUTF8StringEncoding ]]; // 上传上去的图片名字
    [body  appendData :[[ NSString   stringWithString : @"Content-Type: application/octet-stream
    
    " ]  dataUsingEncoding : NSUTF8StringEncoding ]];
    [body  appendData :[ NSData   dataWithData :imageData]];
    [body  appendData :[[ NSString   stringWithFormat : @"
    --%@--
    " ,boundary] dataUsingEncoding : NSUTF8StringEncoding ]];
      [request  setHTTPBody :body];
    // NSLog(@"1-body:%@",body);
    NSLog ( @"2-request:%@" ,request);
    NSData  *returnData = [ NSURLConnection   sendSynchronousRequest :request  returningResponse :nil   error : nil ];
    NSString  *returnString = [[ NSString   alloc ]  initWithData :returnData  encoding :NSUTF8StringEncoding ];
    NSLog ( @"3- 测试输出: %@" ,returnString );
    4.UIlabel多行文字自动换行 (自动折行)
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 180)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 150)];
    label.text = @"Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Helloworld!";
    // 背景颜色为红色
    label.backgroundColor = [UIColor redColor];
    // 设置字体颜色为白色
    label.textColor = [UIColor whiteColor];
    // 文字居中显示
    label.textAlignment = UITextAlignmentCenter;
    // 自动折行设置
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 0;
    5.更改AlertView背景
    UIAlertView *theAlert = [[[UIAlertViewalloc] initWithTitle:@"Atention" 
                                                         message: @"I'm a Chinese!" 
                                                        delegate:nil  
                                                cancelButtonTitle:@"Cancel"  
                                                otherButtonTitles:@"Okay",nil] autorelease]; 
       [theAlert show]; 
       UIImage *theImage = [UIImageimageNamed:@"loveChina.png"];    
       theImage = [theImage stretchableImageWithLeftCapWidth:0topCapHeight:0]; 
       CGSize theSize = [theAlert frame].size; 
        UIGraphicsBeginImageContext(theSize);     
       [theImage drawInRect:CGRectMake(5, 5, theSize.width-10, theSize.height-20)];// 这个地方的大小要自己调整,以适应 alertview 的背景颜色的大小。 
       theImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext(); 
       theAlert.layer.contents = (id)[theImage CGImage];
    6.状态栏的网络活动风火轮是否旋转 
    [UIApplication sharedApplication].networkActivityIndicatorVisible , 默认值是 NO 。

    7.截取屏幕图片

    // 创建一个基于位 图的图形上下文并指定大小为CGSizeMake(200,400)
    UIGraphicsBeginImageContext(CGSizeMake(200,400)); 
     
    //renderInContext  呈现接受者及其子范围到 指定的上下文
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        // 返回 一个基于当前图形上下文的图片
     UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();
      // 移除栈顶 的基于当前位图的图形上下文
    UIGraphicsEndImageContext();
    // 以 png 格式 返回指定图片的数据
    imageData = UIImagePNGR epresentation(aImage);
     
    8.让图片适应框的大小
    NSString*imagePath = [[NSBundle mainBundle] pathForResource:@"XcodeCrash"ofType:@"png"];     
        UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath]; 
            UIImage *newImage= [image transformWidth:80.f height:240.f]; 
        UIImageView *imageView = [[UIImageView alloc]initWithImage: newImage]; 
             [newImagerelease]; 
        [image release]; 
        [self.view addSubview:imageView];

    9.邮箱格式是否正确的代码

    // 利用正则表达式验证
    -( BOOL )isValidateEmail:( NSString  *)email
    {
    NSString  *emailRegex =  @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}" ;
    NSPredicate  *emailTest = [ NSPredicate   predicateWithFormat : @"SELF MATCHES%@",emailRegex];
    return  [emailTest  evaluateWithObject :email];
    
     
     #define CONST_animation_time 0.5
    #define CONST_enlarge_proportion 15.0
     
     
     
    CGPoint UpPointOfView(UIView *view)
    {
     return (CGPoint){view.center.x, 200+2+55/2};
    };
     
    10.放大UIImageView
    - (void) circleAnimate:(UIImageView*)view
    {
     CGContextRef context = UIGraphicsGetCurrentContext();
     [UIView beginAnimations:nil context:context];
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
     [UIView setAnimationDuration:CONST_animation_time];
     [view setCenter:UpPointOfView(view)];
      
      
     CABasicAnimation *scalingAnimation = (CABasicAnimation *)[view.layer animationForKey:@"scaling"];
      
     if (!scalingAnimation)
     {
      scalingAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
      scalingAnimation.repeatCount=1;
      scalingAnimation.duration=CONST_animation_time;
      scalingAnimation.autoreverses=NO;
      scalingAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
      scalingAnimation.fromValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)];
      scalingAnimation.toValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(CONST_enlarge_proportion, CONST_enlarge_proportion, 1.0)];
     }
      
     [view.layer addAnimation:scalingAnimation forKey:@"scaling"];
     view.layer.transform = CATransform3DMakeScale(CONST_enlarge_proportion, CONST_enlarge_proportion, 1.0);
     [UIView commitAnimations];
    }
     
     
    11.GIF图片解析
     

    + (NSMutableArray *)praseGIFDataToImageArray:(NSData *)data; { NSMutableArray *frames = [[NSMutableArray alloc] init]; CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL); CGFloat animationTime = 0.f; if (src) { size_t l = CGImageSourceGetCount(src); frames = [NSMutableArray arrayWithCapacity:l]; for (size_t i = 0; i < l; i++) { CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL); NSDictionary *properties = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(src, i, NULL); NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary]; NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime]; animationTime += [delayTime floatValue]; if (img) { [frames addObject:[UIImage imageWithCGImage:img]]; CGImageRelease(img); } } CFRelease(src); } return frames; }
  • 相关阅读:
    网络与系统安全第四次作业
    2018-2019-1 20189203《linux内核原理与分析》第六周作业
    《网络攻防实践》第五周作业
    《网络攻防实践》第四周作业
    《网络攻防实践》第三周作业
    《网络攻防实践》第二周作业
    《网络攻防实践》第一周作业
    《Linux内核原理与分析》第九周作业
    《Linux内核原理与分析》第八周作业
    《Linux内核原理与分析》第七周作业
  • 原文地址:https://www.cnblogs.com/leeAsia/p/3325019.html
Copyright © 2020-2023  润新知