iOS从相册选择图片进行压缩

@interface ViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>



- (IBAction)chooseImage:(id)sender;


@property (strong, nonatomicUIImageView *imageView;


@end








#import "ViewController.h"

#define kScreenWidth [[UIScreen mainScreen] bounds].size.width

#define kScreenHeight [[UIScreen mainScreen] bounds].size.height


@interface ViewController ()

{

    BOOL isFullScreen;

}

@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.imageView=[[UIImageView alloc] init];

    self.imageView.backgroundColor=[UIColor grayColor];

    self.imageView.frame = CGRectMake(50, 65, 80, 110);

    [self.view addSubview:self.imageView];

    

// Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#pragma mark - 保存图片至沙盒

- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName

{

    

    

     NSData *imageD = UIImageJPEGRepresentation(currentImage, 1);

    

    long   oneLength = [imageD length]/1000;

//    float f=oneLength/1024;

    NSLog(@"%ld",oneLength);

    

    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.1);

    // 获取沙盒目录

 long   length = [imageData length]/1000;

    NSLog(@"%ld",length);

    

    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];

    

    // 将图片写入文件

    

    [imageData writeToFile:fullPath atomically:NO];

}


#pragma mark - image picker delegte

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

[picker dismissViewControllerAnimated:YES completion:^{}];

    

    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    

    [self saveImage:image withName:@"currentImage.png"];

    

    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];

    

    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];

    

    isFullScreen = NO;

    [self.imageView setImage:savedImage];

    

    self.imageView.tag = 100;

    

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

[self dismissViewControllerAnimated:YES completion:^{}];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    isFullScreen = !isFullScreen;

    UITouch *touch = [touches anyObject];

    

    CGPoint touchPoint = [touch locationInView:self.view];

    

    CGPoint imagePoint = self.imageView.frame.origin;

    //touchPoint.x touchPoint.y 就是触点的坐标

    

    // 触点在imageView内,点击imageView 放大,再次点击时缩小

    if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width >=touchPoint.x && imagePoint.y <=  touchPoint.y && imagePoint.y+self.imageView.frame.size.height >= touchPoint.y)

    {

        // 设置图片放大动画

        [UIView beginAnimations:nil context:nil];

        // 动画时间

        [UIView setAnimationDuration:1];

        

        if (isFullScreen) {

            // 放大尺寸

            

            self.imageView.frame = CGRectMake(0, 0, 320, 440);

            self.imageView.backgroundColor=[UIColor clearColor];

        }

        else {

            // 缩小尺寸

            self.imageView.frame = CGRectMake(50, 65, 80, 110);

            self.imageView.backgroundColor=[UIColor clearColor];

        }

        

        // commit动画

        [UIView commitAnimations];

        

    }

    

}


#pragma mark - actionsheet delegate

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (actionSheet.tag == 255) {

        

        NSUInteger sourceType = 0;

        

        // 判断是否支持相机

        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            

            switch (buttonIndex) {

                case 0:

                    // 取消

                    return;

                case 1:

                    // 相机

                    sourceType = UIImagePickerControllerSourceTypeCamera;

                    break;

                    

                case 2:

                    // 相册

                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

                    break;

            }

        }

        else {

            if (buttonIndex == 0) {

                

                return;

            } else {

                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

            }

        }

        // 跳转到相机或相册页面

        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

        

        imagePickerController.delegate = self;

        

        imagePickerController.allowsEditing = YES;

        

        imagePickerController.sourceType = sourceType;

        

        [self presentViewController:imagePickerController animated:YES completion:^{}];

        

        [imagePickerController release];

    }

}

- (IBAction)chooseImage:(id)sender {

    

    UIActionSheet *sheet;

    

    // 判断是否支持相机

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

       {

           sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];

       }

    else {

        

        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];

    }

    

    sheet.tag = 255;

    

    [sheet showInView:self.view];

    

}

- (void)dealloc {

    [_imageView release];

    [super dealloc];

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值