關於圖片的等比縮放,從相機和照片選取頭像,顯示圓形頭像

話不多說,應該都是大家經常使用的功能,就總結了下,分享給朋友們,希望多提寶貴意見

1.通過相機和從相片中選擇設置頭像

//MARK:通過相機和從相片中選擇設置頭像

    //彈出提示框,選擇獲取頭像的方式

    //從相冊獲取圖片

- (void)takePictureClick:(UIButton *)sender

    {

        //    /*注:使用,需要實現以下協議:UIImagePickerControllerDelegate,

        //     UINavigationControllerDelegate

        //     */

        //    UIImagePickerController *picker = [[UIImagePickerController alloc]init];

        //    //設置圖片源(相簿)

        //    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

        //    //設置代理

        //    picker.delegate = self;

        //    //設置可以編輯

        //    picker.allowsEditing = YES;

        //    //打開拾取器界面

        //    [self presentViewController:picker animated:YES completion:nil];

        UIActionSheet* actionSheet = [[UIActionSheetalloc]

                                      initWithTitle:@"請選擇文件來源"

                                      delegate:self

                                      cancelButtonTitle:@"取消"

                                      destructiveButtonTitle:nil

                                      otherButtonTitles:@"照相機",@"攝像機",@"本地相簿",@"本地視頻",nil];

        [actionSheet showInView:self.view];

        

    }


#pragma 點擊按鈕選擇選取圖片的方法

    - (void)actionclickedButtonAtIndex:(NSInteger)buttonIndex

    {

        NSLog(@"buttonIndex = [%ld]",(long)buttonIndex);

        switch (buttonIndex) {

            case0://照相機

            {

                UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

                imagePicker.delegate =self;

                imagePicker.allowsEditing =YES;

                imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;

                //            [self presentModalViewController:imagePicker animated:YES];

                [selfpresentViewController:imagePickeranimated:YEScompletion:nil];

            }

                break;

            case1://攝像機

            {

                UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

                imagePicker.delegate =self;

                imagePicker.allowsEditing =YES;

                imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;

                imagePicker.videoQuality =UIImagePickerControllerQualityTypeLow;

                //            [self presentModalViewController:imagePicker animated:YES];

                [selfpresentViewController:imagePickeranimated:YEScompletion:nil];

            }

                break;

            case2://本地相簿

            {

                UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

                imagePicker.delegate =self;

                imagePicker.allowsEditing =YES;

                imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

                //            [self presentModalViewController:imagePicker animated:YES];

                [selfpresentViewController:imagePickeranimated:YEScompletion:nil];

            }

                break;

            case3://本地視頻

            {

                UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

                imagePicker.delegate =self;

                imagePicker.allowsEditing =YES;

                imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

                //            [self presentModalViewController:imagePicker animated:YES];

                [selfpresentViewController:imagePickeranimated:YEScompletion:nil];

            }

                break;

            default:

                break;

        }

    }

#pragma mark -

#pragma UIImagePickerController Delegate方法

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

    {

        if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kTTypeImage]) {

            UIImage *img = [infoobjectForKey:UIImagePickerControllerEditedImage];

            [selfperformSelector:@selector(saveImage:) withObject:img afterDelay:0.5];

        }

        elseif ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeMovie]) {

            NSString *videoPath = [[infoobjectForKey:UIImagePickerControllerMediaURL]path];

            self.fileData = [NSData dataWithContentsOfFile:videoPath];

        }

        //    [picker dismissModalViewControllerAnimated:YES];

        [picker dismissViewControllerAnimated:YEScompletion:nil];

    }

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

    {

        //    [picker dismissModalViewControllerAnimated:YES];

        [picker dismissViewControllerAnimated:YEScompletion:nil];

    }

    - (void)saveImage:(UIImage *)image {

        //    NSLog(@"保存頭像!");

        //    [userPhotoButton setImage:image forState:UIControlStateNormal];

        BOOL success;

        NSFileManager *fileManager = [NSFileManagerdefaultManager];

        NSError *error;

        

        NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

        NSString *documentsDirectory = [pathsobjectAtIndex:0];

        NSString *imageFilePath = [documentsDirectorystringByAppendingPathComponent:@"selfPhoto.jpg"];

        NSLog(@"imageFile->>%@",imageFilePath);

        success = [fileManager fileExistsAtPath:imageFilePath];

        if(success) {

            success = [fileManager removeItemAtPath:imageFilePatherror:&error];

        }

        //    UIImage *smallImage=[self scaleFromImage:image toSize:CGSizeMake(80.0f, 80.0f)];//將圖片尺寸改爲80*80

        UIImage *smallImage = [selfthumbnailWithImageWithoutScale:imagesize:CGSizeMake(93,93)];

        [UIImageJPEGRepresentation(smallImage,1.0f) writeToFile:imageFilePathatomically:YES];//寫入文件

        UIImage *selfPhoto = [UIImageimageWithContentsOfFile:imageFilePath];//讀取圖片文件

        //    [userPhotoButton setImage:selfPhoto forState:UIControlStateNormal];

        self.imv.image = selfPhoto;

    }

    // 改變圖像的尺寸,方便上傳服務器

    - (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size

    {

        UIGraphicsBeginImageContext(size);

        [image drawInRect:CGRectMake(0,0, size.width, size.height)];

        UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return newImage;

    }

//MARK: 2.保持原始圖片的長寬比,生成需要尺寸的圖片

    //2.保持原來的長寬比,生成一個縮略圖

    - (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize

    {

        UIImage *newimage;

        if (nil == image) {

            newimage = nil;

        }

        else{

            CGSize oldsize = image.size;

            CGRect rect;

            if (asize.width/asize.height > oldsize.width/oldsize.height) {

                rect.size.width = asize.height*oldsize.width/oldsize.height;

                rect.size.height = asize.height;

                rect.origin.x = (asize.width - rect.size.width)/2;

                rect.origin.y =0;

            }

            else{

                rect.size.width = asize.width;

                rect.size.height = asize.width*oldsize.height/oldsize.width;

                rect.origin.x =0;

                rect.origin.y = (asize.height - rect.size.height)/2;

            }

            UIGraphicsBeginImageContext(asize);

            CGContextRef context =UIGraphicsGetCurrentContext();

            CGContextSetFillColorWithColor(context, [[UIColorclearColor] CGColor]);

            UIRectFill(CGRectMake(0,0, asize.width, asize.height));//clear background

            [image drawInRect:rect];

            newimage = UIGraphicsGetImageFromCurrentImageContext();

            UIGraphicsEndImageContext();

        }

        return newimage;

    }



//MARK:3.顯示圓形頭像

- (void)showRound{

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [pathsobjectAtIndex:0];

    NSString *imageFilePath = [documentsDirectorystringByAppendingPathComponent:@"selfPhoto.jpg"];

    NSLog(@"imageFile->>%@",imageFilePath);

    UIImage *selfPhoto = [UIImageimageWithContentsOfFile:imageFilePath];

    self.imv.image = selfPhoto;

    [self.imv.layersetCornerRadius:CGRectGetHeight([self.imvbounds]) / 2]; //修改半徑,實現頭像的圓形化

    self.imv.layer.masksToBounds = YES;

    

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章