您的位置:首页 > 移动开发 > IOS开发

开发笔记 那些年追过的图片(二): 读取相册里的图片 和 拍摄获取照片

2013-09-12 14:43 253 查看
站在巨人的肩膀我们会看见山后面的海

获取照片我们要用到ios sdk    UIImagePickerController 
很简单直接上代码

//    typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType)
//  {
//        UIImagePickerControllerSourceTypePhotoLibrary,      //同步到iphone的图片以及包括用户拍摄的图片在内的任何相册
//        UIImagePickerControllerSourceTypeCamera,            //拍照
//        UIImagePickerControllerSourceTypeSavedPhotosAlbum   //仅含相册
//    };

//  ViewController.h
//  ImageDemo
//
//  Created by lengshengren on 13-9-12.
//  Copyright (c) 2013年 lengshengren. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController :UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImagePickerController * imagePicker;
}

@property (nonatomic,strong)UIImagePickerController *imagePicker;
@property (weak, nonatomic)IBOutletUIImageView *imageView;
- (IBAction)getIMage:(id)sender;

@end

- (IBAction)getIMage:(id)sender
{
    
UIActionSheet  *actionSheet = [[UIActionSheetalloc]initWithTitle:@""
                                             delegate:self
                                    cancelButtonTitle:@"取消"
                               destructiveButtonTitle:nil
                                    otherButtonTitles:@"拍照",@"从相册选取",nil];
    [actionSheet showInView:self.view];
    

}

-
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    imagePicker = [[UIImagePickerControlleralloc]
init];
    imagePicker.delegate =self;
    imagePicker.allowsEditing =YES;
    imagePicker.modalTransitionStyle =UIModalTransitionStyleCoverVertical;
    //照相上传
    if (buttonIndex == 0)
    {
        imagePicker.sourceType =UIImagePickerControllerSourceTypeCamera;
     }
    //相册上传
    else if (buttonIndex ==1)
    {
         imagePicker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
     }
    
    [self  presentViewController:imagePicker animated:YES  completion:nil];

    
}

#pragma mark-
#pragma mark 照片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    
    _imageView.image = image;

    //关闭相册界面
    [picker dismissModalViewControllerAnimated:YES];
}
@end

//压缩图片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios image