我在iOS 3.1.3 iPhone上测试我的iPhone应用程序.我使用以下方法选择/捕获图像UIImagePickerController:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self.navigationController presentModalViewController:imagePicker animated:YES];
[imagePicker release];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = self.image;
[self.navigationController dismissModalViewControllerAnimated:YES];
submitButton.enabled = YES;
}
Run Code Online (Sandbox Code Playgroud)
然后我在某个时候使用ASI类将它发送到我的Web服务器:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://example.com/myscript.php"]];
[request setDelegate:self];
[request setStringEncoding:NSUTF8StringEncoding];
[request setShouldContinueWhenAppEntersBackground:YES];
//other post keys/values
[request setFile:UIImageJPEGRepresentation(self.image, 100.0f) withFileName:[NSString stringWithFormat:@"%d.jpg", [[NSDate date] timeIntervalSinceNow]] andContentType:@"image/jpg" forKey:@"imageFile"];
[request startAsynchronous];
Run Code Online (Sandbox Code Playgroud)
问题:当我用iphone拍照同时保持它的风景时,图像会上传到服务器并按照你的预期进行查看.当拍摄手机拍照时,图像会被上传并在旋转90度时被观看.
我的应用程序设置为仅在纵向模式下工作(颠倒和常规).
如何在上传后使图像始终显示正确的方向?
UIImageView中显示的图像看起来是正确的(在拍照后直接显示),但在服务器上查看则另有说明.
我使用以下代码来保存和加载我从库中选择的图像或使用相机:
//saving an image
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally …Run Code Online (Sandbox Code Playgroud) 在Objc中,我CGImageRelease在处理图像后使用方法.但在Swift中,这种方法不可用.
关于Apple文档之后Retaining and Releasing Images有一个2 Objective-C symbols hidden
我的问题是,为什么CGImageReleaseSwift中没有更多?我们要调用另一种方法来取代它吗?
谢谢 !
我试图使用CGImageCreateWithImageInRect从图像中获取子图像,但它无法正常工作.它从图像中的随机位置返回图像,而不是我指定的图像.请看下面的图片;

我的代码是:
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(524, 1331, 600, 600));
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我究竟做错了什么?
谢谢!
假设你从一个UIImage开始,你想要裁剪它.最常见的方法是使用CGImageCreateWithImageInRect来获取CGImageRef并从中创建一个新图像,如下所示:
CGRect cropRect = CGRectMake(x*width, y*height, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
Run Code Online (Sandbox Code Playgroud)
现在让我们说你以后需要将这个UIImage转换为NSData对象.例如,如果您希望使用NSKeyedArchiver将映像存储在磁盘上,则会发生这种情况.或者,您可以通过以下显式执行以下操作来获取NSData对象:
NSData *imageData = UIImagePNGRepresentation(croppedImage);
Run Code Online (Sandbox Code Playgroud)
我做了这个,后来我尝试通过以下方式重建一个新的UIImage对象:
UIImage *image = [UIImage imageWithData:imageData];
Run Code Online (Sandbox Code Playgroud)
我为一系列48张图片做了这个.他们中的大多数都很好,但是在每4张图片之后,接下来的两张图片都被破坏了,每张图片的顶部和底部都被交换了.如果我将转换转换为NSData并再次返回,它可以正常工作.如果我使用NSKeyedArchiver将图像数组写入磁盘(我猜它在其编码例程中调用类似UIImagePNGRepresentation的东西),我也会得到完全相同的问题.我的理论是这必须与我如何从CGImageRefs构建图像有关.
这是什么解决方法?我似乎无法弄清楚如何将CGImageRefs放入NSData对象,我更喜欢一个正确构建UIImage的解决方案,以便它可以被序列化.
编辑:
有些人要求运行实际的代码,所以我在这里添加它
UIImage *image = self.animationObject.image;
for (int y=0; y<[self.animationInfoObject.numInY intValue]; y++) {
for (int x=0; x<[self.animationInfoObject.numInX intValue]; x++) {
CGRect cropRect = CGRectMake(x*width, y*height, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
NSData *imageData = UIImageJPEGRepresentation(croppedImage, 1.0);
[self addImageToArray:imageData];
CFRelease(imageRef);
}
} …Run Code Online (Sandbox Code Playgroud) ios ×3
iphone ×3
objective-c ×3
uiimage ×3
cocoa-touch ×2
cgimage ×1
cgimageref ×1
ipad ×1
nsdata ×1
orientation ×1
swift ×1
uiimageview ×1