保存图片到本地,查看本地图片,递归实现scrollView的无限循环滚动,文字横向渐变效果,autoLayout

米嘉禧
2023-12-01
//
//  ViewController.m
//  DiGui
//
//  Created by test on 16/3/28.
//  Copyright © 2016年 Zhou. All rights reserved.
//

#pragma mark —————————— AutoLayout----添加两个约束实现imageView居中,并设置宽高比为1:2。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
    [self.view addSubview:imageView];
    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    //No.1
    //开始写代码,为imageView添加两个约束实现imageView居中,并设置宽高比为1:2。
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0]];
    
    
    //end_code
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

#pragma mark ————————  CAGradientLayer和CADisplayLink两个类实现文字的颜色渐变

/*
#import "ViewController.h"
@interface ViewController ()
{
    CAGradientLayer *_gradientLayer;
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label=[[UILabel alloc]init];
    label.text=@"药药切克闹,煎饼果子来一套!";
    label.font=[UIFont systemFontOfSize:25];
    [label sizeToFit];
    label.center=CGPointMake(200, 100);
    [self.view addSubview:label];
    //No.1
    //开始写代码,实现文字横向渐变效果,颜色:通过randomColor方法随机。利用定时器(CADisplayLink)快速的切换渐变颜色,定时器的方法已经给出(textColorChange)。
    _gradientLayer = [CAGradientLayer layer];
    
    
    [self.view.layer addSublayer:_gradientLayer];
    
    // 利用定时器,切换渐变颜色
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(textColorChange)];
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    
    _gradientLayer.frame = label.frame;
    _gradientLayer.mask = label.layer;
    
    _gradientLayer.startPoint = CGPointMake(0.0, 0.5);
    _gradientLayer.endPoint = CGPointMake(1.0, 0.5);
    
    label.frame = _gradientLayer.bounds;
    
    
    //end_code
}
-(UIColor *)randomColor{
    CGFloat r=arc4random_uniform(256)/255.0;
    CGFloat g=arc4random_uniform(256)/255.0;
    CGFloat b=arc4random_uniform(256)/255.0;
    return [UIColor colorWithRed:r green:g blue:b alpha:1];
}
-(void)textColorChange
{
    _gradientLayer.colors = @[ (id)[self randomColor].CGColor,
                               (id)[self randomColor].CGColor,
                               (id)[self randomColor].CGColor,
                               (id)[self randomColor].CGColor,
                               (id)[self randomColor].CGColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
*/
 
 
#pragma mark ———————— 实现八张图片连续滚动到最后一张之后按相反的顺序再滚回到第一张,循环滚动不结束。

/*

#import "ViewController.h"

@interface ViewController (){
    UIScrollView *mainScroll;
    BOOL isFinish;
    int x;
}

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    x=0;
    isFinish = YES;
    
    mainScroll = [[UIScrollView alloc]init];
    mainScroll.frame = self.view.bounds;
    mainScroll.contentSize = CGSizeMake(self.view.bounds.size.width*8, self.view.bounds.size.height);
    
    for(int i = 0; i < 8; i++){
        UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]]];
       
        imgView.backgroundColor = [UIColor colorWithRed:random() % 256 /255.0 green:random() % 256 /255.0 blue:random() %256 /255.0 alpha:1.0];
        
        imgView.frame = CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        [mainScroll addSubview:imgView];
    }
    [self.view addSubview:mainScroll];
    [self moveScroll];
    
    
}
  // isfinish yes 从0——8  NO 8——0
//No.1
//开始写代码,实现八张图片连续滚动到最后一张之后按相反的顺序再滚回到第一张,循环滚动不结束。
-(void)moveScroll
{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self DiGui:7];
        
    });

}
- (void)DiGui:(int)time
{
    if (isFinish) {
        x = 0;
    }else
    {
        x = 7;
    }
    //  0   0,0   7  7,0
     if (time ==  x ) {
            
       [mainScroll setContentOffset:CGPointMake(x * self.view.frame.size.width, 0) animated:YES];
            
          return;
     }
        
        // -1  +1
    if (isFinish) {
       [self DiGui:time-1];
    }
    else
    {
        [self DiGui:time + 1];
    }
        
        sleep(1);
        
        [mainScroll setContentOffset:CGPointMake(self.view.frame.size.width*time, 0) animated:YES];
        
        sleep(1);
        //  7   0    0 7
        if (time == 7 - x) {
            
            isFinish = !isFinish;
            [self DiGui:x];
            
        }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
*/

#pragma mark ———————— 把图片切成圆的并加上红色边框

/*
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView * portaitImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    portaitImageView.frame = CGRectMake(0, 0, 200, 200);
    portaitImageView.center = self.view.center;
    portaitImageView.contentMode = UIViewContentModeScaleAspectFill;
    portaitImageView.backgroundColor = [UIColor blackColor];
    //No.1
    //开始写代码,把图片切成圆的并加上红色边框。
    portaitImageView.layer.cornerRadius = 100;
   // portaitImageView.layer.backgroundColor = [UIColor redColor].CGColor;
    portaitImageView.layer.borderWidth = 2;
    portaitImageView.layer.masksToBounds = YES;
  portaitImageView.layer.borderColor = [UIColor redColor].CGColor;
    
    
    
    
    
    //end_code
    [self.view addSubview:portaitImageView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
 */

#pragma mark ———————— 图片存在本地相册中,并从本地相册中查看图片
/*
#import "ViewController.h"

@interface ViewController ()<UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)transferToAlbum:(id)sender;
- (IBAction)save:(UIButton *)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (IBAction)transferToAlbum:(id)sender {
    [self presentImagePickerController];
}

//No.1
//开始写代码,将图片存在本地相册中,保存结束执行@selector(image:didFinishSavingWithError:contextInfo:)方法
- (IBAction)save:(UIButton *)sender
{
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
   
}


//end_code

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if(error){
        [[[UIAlertView alloc]initWithTitle:@"提示" message:@"存入相册失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil] show];
    }else{
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"已存入相册,是否查看" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.delegate = self;
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self presentImagePickerController];
    }
}

//No.2
//开始写代码,点击确定去相册查看图片
- (void)presentImagePickerController

{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:imagePickerController animated:YES completion:^{
        
    }];
    
    
 
}






//end_code

#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    _imageView.image = image;
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
*/

 类似资料: