苹果笔记本怎么把文件拖到文件夹UIimage文件夹里的是什么地方的文件

UIImagePickerController在iPhone和iPad中用法的一点不同[转]
我们知道,在iPhone中获取照片库常用的方法如下:
UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoL
m_imagePicker.delegate =
[m_imagePicker.navigationBar.subviews];
[m_imagePicker setAllowsEditing:NO];
//m_imagePicker.allowsImageEditing = NO;
[self presentModalViewController:m_imagePicker animated:YES];
[m_imagePicker release];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
这对iPhone的操作是没有问题的。但是当我们在iPad环境中却有问题了,当我们运行时会报如下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'
所以我们必须通过UIPopoverController来实现才行。具体实现如下:
UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoL
m_imagePicker.delegate =
[m_imagePicker setAllowsEditing:NO];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
self.popoverController =
//popoverController.delegate =
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//[self presentModalViewController:m_imagePicker animated:YES];
[popover release];
[m_imagePicker release];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[alert show];
[alert release];
这里需要注意,对局部UIPopoverController对象popover我们赋给了一个全局的UIPopoverController对象popoverController。而不能直接调用popover。因为在popover对象还可见时,是不能够被释放的。
欢迎加群互相学习,共同进步。QQ群:iOS:
| Android:
| Go: | Python: | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive//2526622.html
ghost丶桃子UIImagePickerController在iPhone6s真机下重压(3D Touch)相册中的图片,直接闪退
[问题点数:100分]
本版专家分:0
CSDN今日推荐
本版专家分:5740
2016年3月 移动开发大版内专家分月排行榜第三
本版专家分:0
本版专家分:0
本版专家分:0
本版专家分:0
匿名用户不能发表回复!|
其他相关推荐IOS把图片缓存到本地的几种方法
把图片缓存到本地,在很多场景都会用到,如果是只储存文字信息,那建一个plist文件,或者就能很方便的解决问题,但是如果存图片到沙盒就没那么方便了。这里介绍两种保存图片到沙盒的方法。
一.把图片转为base64的字符串存到数据库中或者plist文件中,然后用到的时候再取出来
//获取沙盒路径,
NSString *path_sandox = NSHomeDirectory();
//创建一个存储plist文件的路径
NSString *newPath = [path_sandox stringByAppendingPathComponent:@/Documents/pic.plist];
NSMutableArray *arr = [[NSMutableArray alloc] init];
//把图片转换为Base64的字符串
NSString *image64 = [self encodeToBase64String:image];
[arr addObject:image64];
//写入plist文件
if ([arr writeToFile:newPath atomically:YES]) {
NSLog(@写入成功);
这样就存起来的,然后用到的时候再利用存储的字符串转化为图片
NSData *_decodedImageData
= [[NSData alloc] initWithBase64Encoding:image64];
UIImage *_decodedImage
= [UIImage imageWithData:_decodedImageData];
二.把图片直接保存到沙盒中,然后再把路径存储起来,等到用图片的时候先获取图片的路径,再通过路径拿到图片
//拿到图片
UIImage *image = [UIImage imageNamed:@flower.png]; NSString *path_sandox = NSHomeDirectory();
//设置一个图片的存储路径
NSString *imagePath = [path_sandox stringByAppendingString:@/Documents/flower.png];
//把图片直接保存到指定的路径(同时应该把图片的路径imagePath存起来,下次就可以直接用来取)
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
下次利用图片的地址直接来拿图片。
同时附上获取沙盒目录的代码
沙盒文件目录获取代码:
//Home目录
NSString *homeDirectory = NSHomeDirectory();
//Document目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Cache目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Libaray目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];UIImage - UIKit | Apple Developer Documentation
ClassAn object that manages image data in your app.
SDKsiOS 2.0+tvOS 9.0+watchOS 2.0+FrameworkUIKitOverviewYou use image objects to represent image data of all kinds, and the UIImage class is capable of managing data for all image formats supported by the underlying platform. Image objects are immutable, so you always create them from existing image data, such as an image file on disk or programmatically created image data. An image object may contain a single image or a sequence of images you intend to use in an animation.You can use image objects in several different ways:Assign an image to a
object to display the image in your interface.Use an image to customize system controls such as buttons, sliders, and segmented controls. Draw an image directly into a view or other graphics context.Pass an image to other APIs that might require image data. Although image objects support all platform-native image formats, it is recommended that you use PNG or JPEG files for most images in your app. Image objects are optimized for reading and displaying both formats, and those formats offer better performance than most other image formats. Because the PNG format is lossless, it is especially recommended for the images you use in your app’s interface.Creating Image ObjectsWhen creating image objects using the methods of this class, you must have existing image data located in a file or data structure. You cannot create an empty image and draw content into it. There are many options for creating image objects, each of which is best for specific situations:Use the
method (or the init(named:) method) to create an image from an image asset or image file located in your app’s main bundle (or some other known bundle). Because these methods cache the image data automatically, they are especially recommended for images that you use frequently.Use the
method to create an image object where the initial data is not in a bundle. These methods load the image data from disk each time, so you should not use them to load the same image repeatedly. Use the
methods to create a single UIImage object comprised of multiple sequential images. Install the resulting image in a
object to create animations in your interface.Other methods of the UIImage class let you create animations from specific types of data, such as Core Graphics images or image data that you create yourself. UIKit also provides the
function for creating images from content that you draw yourself. You use that function in conjunction with a bitmap-based graphics context, which you use to capture your drawing commands.Image assets are the easiest way to manage the images that ship with your app. Each new Xcode project contains an assets library, to which you can add multiple image sets. An image set contains the variations of a single image that your app uses. A single image set can provide different versions of an image for different platforms, for different trait environments (compact or regular), and for different scale factors. In addition to loading images from disk, you can ask the user to supply images from an available camera or photo library using a
object. An image picker displays a custom user interface for selecting images. Accessing user-supplied images requires explicit user permission. For more information about using an image picker, see . Defining a Stretchable ImageA stretchable image is one that defines regions where the underlying image data can be duplicated in an aesthetically pleasing way. Stretchable images are commonly used to create backgrounds that can grow or shrink to fill the available space.You define a stretchable image by adding insets to an existing image using the
method. The insets subdivide the image into two or more parts. Specifying nonzero values for each inset yields an image divided into nine parts, as shown in Figure 1. Figure 1 Using insets to define stretchable regionsEach inset defines the portion of the image that does not stretch in the given dimension. The regions inside an image’s top and bottom insets maintain a fixed height, and the areas inside the left and right insets maintain a fixed width. Figure 2 shows how each part of a nine-part image stretches as the image itself is stretched to fill the available space. The corners of the image do not change size because they are inside both a horizontal and vertical inset. Figure 2 Stretchable portions of a nine-part imageComparing ImagesThe
method is the only reliable way to determine whether two images contain the same image data. The image objects you create may be different from each other, even when you initialize them with the same cached image data. The only way to determine their equality is to use the isEqual(_:) method, which compares the actual image data. Listing 1 illustrates the correct and incorrect ways to compare images.
Listing 1 Comparing two imageslet image1 = UIImage(named: &MyImage&)
let image2 = UIImage(named: &MyImage&)
if image1 != nil && image1!.isEqual(image2) {
// Correct. This technique compares the image data correctly.
if image1 == image2 {
// Incorrect! Direct object comparisons may not work.
Accessing the Image DataImage objects not provide direct access to their underlying image data. However, you can retrieve the image data in other formats for use in your app. Specifically, you can use the
properties to retrieve versions of the image that are compatible with Core Graphics and Core Image, respectively. You can also use the
functions to generate an NSData object containing the image data in either the PNG or JPEG format. TopicsLoading and Caching ImagesCreating and Initializing Image ObjectsCreating Specialized Image ObjectsGetting the Image DataGetting the Image Size and ScaleImage AttributesGetting Rendering InformationDrawing ImagesDeprecatedDeprecatedDeprecatedDeprecatedInstance MethodsRelationshipsInherits FromConforms Toiphone - How do I save a UIImage to a file? - Stack Overflow
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our , , and our .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
If I have a UIImage from an imagePicker, how can I save it to a subfolder in the documents directory?
8,56343345
Of course you can create subfolders in the documents folder of your app. You use
to do that.
You use UIImagePNGRepresentation to convert your image to NSData and save that to disk.
// Create path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];
// Save image.
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
Core Data has nothing to do with saving images to disk by the way.
4,37711828
34.7k888123
In Swift 3:
// Create path.
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let filePath = "\(paths[0])/MyImageName.png"
// Save image.
UIImagePNGRepresentation(image)?.writeToFile(filePath, atomically: true)
4,61342425
You have to
(say, JPEG or PNG), and then call writeToFile:atomically: on the representation:
UIImage *image = ...;
*path = ...;
[UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];
590k547251150
The above are useful, but they don't answer your question of how to save in a subdirectory or get the image from a UIImagePicker.
First, you must specify that your controller implements image picker's delegate, in either .m or .h code file, such as:
@interface CameraViewController () &UIImagePickerControllerDelegate&
Then you implement the delegate's imagePickerController:didFinishPickingMediaWithInfo: method, which is where you can get the photograph from the image picker and save it (of course, you may have another class/object that handles the saving, but I'll just show the code inside the method):
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
// get the captured image
UIImage *image = (UIImage *)info[UIImagePickerControllerOriginalImage];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imageSubdirectory = [documentsDirectory stringByAppendingPathComponent:@"MySubfolderName"];
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.png"];
// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to PNG spec
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];
If you want to save as JPEG image, the last 3 lines would be:
NSString *filePath = [imageSubdirectory stringByAppendingPathComponent:@"MyImageName.jpg"];
// Convert UIImage object into NSData (a wrapper for a stream of bytes) formatted according to JPG spec
NSData *imageData = UIImageJPEGRepresentation(image, 0.85f); // quality level 85%
[imageData writeToFile:filePath atomically:YES];
3,73831724
extension UIImage {
/// Save PNG in the Documents directory
func save(_ name: String) {
let path: String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let url = URL(fileURLWithPath: path).appendingPathComponent(name)
try! UIImagePNGRepresentation(self)?.write(to: url)
print("saved image at \(url)")
// Usage: Saves file in the Documents directory
image.save("climate_model_2017.png")
28.8k18124115
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:path atomically:YES];
where path is the name of the file you want to write it to.
4,04863259
First you should get the Documents directory
/* create path to cache directory inside the application's Documents directory */
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"fileName"];
Then you should save the photo to the file
NSData *photoData = UIImageJPEGRepresentation(photoImage, 1);
[photoData writeToFile:filePath atomically:YES];
4,19393473
In Swift 4:
// Create path.
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
if let filePath = paths.first?.appendingPathComponent("MyImageName.png") {
// Save image.
try UIImagePNGRepresentation(image)?.write(to: filePath, options: .atomic)
// Handle the error
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
Post Your Answer
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 苹果笔记本怎么把文件拖到文件夹 的文章

 

随机推荐