我想保存UIImage到NSData,然后读NSData回一个新UIImage的雨燕.要使用以下代码将其转换UIImage为NSData我:
let imageData: NSData = UIImagePNGRepresentation(myImage)
Run Code Online (Sandbox Code Playgroud)
如何将imageData(即NSData)转换回新的UIImage?
从iOS 9和Xcode 7开始,我无法再在iPad(设备和模拟器)上实现UIImagePickerController.以下代码适用于iPad,但仅适用于iOS 9.使用iOS 9+时,显示的图像(在UIImagePickerController被取消后)是所选图像的错误版本.没有重新调整大小或裁剪最终图像只是原始图像的右上角?? 另外一个问题 - 如果imagePicker.allowsEditing = false,你无法从PhotoLibrary中选择图像?
@IBAction func photoButton(sender: AnyObject) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: false, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
self.imageView.image = pickedImage
dismissViewControllerAnimated(true, completion: { () -> Void in
})
}
Run Code Online (Sandbox Code Playgroud)
下面是UIImagePickerController中显示的选定图像的示例.(注意所选图像如何呈现非常小而不像以前那样呈现屏幕的全尺寸/宽度)
在UIImagePickerController中选择使用按钮后,最终图像仅在原始图像的右上角.我在做错了什么或是在iOS 9上破坏了UIImagePickerController?
我正在学习Swift,我可以理解如何创建一个接收数组并返回数组的简单函数.继承我的代码:
func myArrayFunc(inputArray:Array) -> Array{
var newArray = inputArray
// do stuff with newArray
return newArray
}
Run Code Online (Sandbox Code Playgroud)
我得到的红色错误是:对泛型类型'Array'的引用需要<>中的参数
我的应用程序中有UIScrollView,启用了垂直弹跳.我需要在屏幕底部弹跳但不在屏幕顶部启用.
我没有如何检测scrollview与以下内容:
(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
// no bounce if scrollView has reached the top of the screen ??
}
Run Code Online (Sandbox Code Playgroud) 我有一个最初在目标C中创建的应用程序(让我们调用此版本1),我现在已将我的应用程序转换为Swift 2.0(版本2).该应用程序的主要功能之一是能够将图像和文本从一个设备发送到另一个设备.图像和文本存储在NSMutableDictionary中,然后编码为NSData,并在Parse后端服务器上发送/存储.我的应用程序的设计还能够将图像从一个设备通过电子邮件发送到另一个设备.
这适用于我的应用程序的两个版本 - Objective C和Swift.好极了!
我的问题是当用户从我的应用程序的版本1发送NSData到版本2的设备(基本上是在目标C中编码然后在Swift中解码的图像)!编码文本解码很好但不是图像(保存为objectForKey("data")).见下面的例子.quizData是一个数组,它保存从另一个设备发送的字典(keyValue项).此数组适用于除objectForKey("data")之外的所有项目.该对象是编码图像.
var imageData = NSData()
imageData = quizData.objectAtIndex(currentQuestionNumber).objectForKey("data") as! NSData
// the following always prints out lots of info to confirm the imageData has the encoded image
print("imageData.length = \(imageData.length)")
print("imageData.description = \(imageData.description)")
// decoding
photoImageView.image = UIImage(data:imageData)
Run Code Online (Sandbox Code Playgroud)
好的,所以当使用Swift在另一台设备上创建图像时,上述工作正常.但是如果从版本1(目标c)创建和发送的图像是photoImageView是空白的(没有错误),那么imageData是巨大的(打印输出显示imageDate确实保存用户图像).
如果NSdata对象具有UIImage的数据,它应该能够在ObjC或Swift中解码吗?如果需要,发送更多代码没问题
问题修改如下:不确定这是否真的有帮助,但是通过电子邮件发送NSData的objC代码(所有应用程序数据都保存为pList)
// emailArray to be populated with selected data from plist
NSMutableArray *emailArray = [NSMutableArray arrayWithContentsOfFile:path];
MFMailComposeViewController *emailPicker = [[MFMailComposeViewController alloc]init];
emailPicker.mailComposeDelegate =self;
/// NSdata from emailArray
NSData *emailQuizData = [NSKeyedArchiver archivedDataWithRootObject:emailArray];
[emailPicker addAttachmentData:emailQuizData …Run Code Online (Sandbox Code Playgroud) 我一直在使用Image.xcassets 跟随本教程https://www.youtube.com/watch?v=_36Y6rDcKP0在不同设备上显示全屏图像.因为可以清楚地显示placeHolders,所以创建启动项非常容易.但是,我的问题是在创建新的图像集时如下.

我在每个地方放置的图像如下:
我的问题是,当我运行iPhone 6模拟器时,它会加载bg@2x.png(640 x 960)图像而不是iPhone 6的bg-568h@2x.png(640 x 1136)图像?(图像被拉伸).所有其他图像尺寸对于每个设备都是正确的.在视频教程中,iPhone 6模拟器确实加载了bg-568h@2x.png(640 x 1136).我究竟做错了什么 ??
在应用程序购买中,我希望本地货币符号显然适合用户本地货币.使用NSLocaleCurrencySymbol作为检测用户所在位置的主要来源是否安全.这是我的代码的一部分:
NSLocale *theLocale = [NSLocale currentLocale];
NSString *symbol = [theLocale objectForKey:NSLocaleCurrencySymbol];
NSString *cost = [NSString stringWithFormat:@"%@",product.price];
Run Code Online (Sandbox Code Playgroud)
默认情况下,当用户购买iOS设备时,在每台设备的国际设置中自动完成正确的区域格式?
我正在学习如何将NSDate对象与isEqualToDateDate方法进行比较.我不明白为什么这个简单的测试不会返回true并打印出来NSLog.提前致谢
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDate *myDate = [[NSDate alloc]initWithTimeIntervalSinceNow:4000000];
NSDate *otherDate = [[NSDate alloc]initWithTimeIntervalSinceNow:4000000];
NSLog(@"myDate %@",myDate);
NSLog(@"otherDate %@",otherDate);
if ([myDate isEqualToDate:otherDate]) {
NSLog(@"The dates are the same");
};
[myDate release];
[otherDate release];
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud) 使用故事板和UINavigationController我的应用程序有很多UIViewControllers.初始UIViewController名为viewController,是一种主页.
如果用户离开应用程序并返回,我总是希望用户UIViewController在离开之前总是返回主页()而不是最后一个视图.在我appDelegate,我如何调用/显示我的主页(UIViewController)applicationDidBecomeActive: ?
我的通用应用程序有两个故事板.一个用于iPhone,另一个用于iPad.许多代码都是以编程方式编写的,而且两个storyBoards共享相同的ViewControllers.在视图控制器中如何工作的示例如下:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ iPadInUse = YES;}
else { iPadInUse = NO;}
Run Code Online (Sandbox Code Playgroud)
所以,如果我有一个需要在两个设备上显示的图像 - 我写了以下内容:
if (iPadInUse == YES) { recordViewImage = @"iPadRecordPanel@2x.png"
// x and y positions to adjusted accordingly }
else { recordViewImage = @"recordingPanel@2x.png"
// // x and y positions to adjusted accordingly}
Run Code Online (Sandbox Code Playgroud)
在模拟器上,一切都适用于所有设备(视网膜/非视网膜).我唯一的实际设备是iPhone 4S(一切正常).这是正常我正在做什么,因为实际上没有任何非视网膜图像加载非视网膜设备(所有图像后缀与@ 2x.png?我的应用程序会被拒绝因为这?
使用Xcode 6.1我正在尝试创建一个从左到右的自定义segue过渡.我按照本教程点击这里.在将类NSObject更改为UIStoryboardSegue时,我陷入困境,如下所示:
#import <Foundation/Foundation.h>
@interface ZHCustomSegue : NSObject
// unable to change NSObject to UIStoryboardSegue
@end
Run Code Online (Sandbox Code Playgroud)
我收到一条红色错误,上面写着'无法找到UIStoryboardSegue的界面声明'
我尝试使用 UIActivityViewController 通过 AirDrop 将数据(NSData)从一台 iOS 设备上的应用程序发送到另一台设备。我在我的应用程序 plist 中创建了一个新的 CSM(自定义数据类型)。public.filename-extension = ppm。那么如何将 ppm 扩展添加到我尝试发送的 NSDate 对象中?我是否正确地认为,当您呈现 UIActivityViewController 时,如果我发送的对象没有我的应用程序公共扩展名(ppm),我的应用程序图标将不会显示在 UIActivityViewController 窗口中??....是的,我是真的很困惑!这是我用来呈现 UIActivityViewController 的代码
@IBAction func shareButton(sender: AnyObject) {
// myData is the object I want to send to be used in my app on another device
let vc = UIActivityViewController(activityItems: [myData],applicationActivities: [])
presentViewController(vc, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想做的就是发送要在我的应用程序中使用的自定义数据
我正在尝试学习NSTimer,使用Foundation和打印到控制台.任何人都可以告诉我我需要做些什么来让以下工作?它编译没有错误,但不激活我的startTimer方法 - 没有打印.
我的目标是让一个方法调用另一个方法来运行一些语句,然后在设定的时间后停止.
#import <Foundation/Foundation.h>
@interface MyTime : NSObject {
NSTimer *timer;
}
- (void)startTimer;
@end
@implementation MyTime
- (void)dealloc {
[timer invalidate];
[super dealloc];
}
- (void)startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(runTimer:) userInfo:nil repeats:YES];
}
- (void)runTimer:(NSTimer *)aTimer {
NSLog(@"timer fired");
}
@end
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MyTime *timerTest = [[MyTime alloc] init];
[timerTest startTimer];
[timerTest release];
[pool release];
return 0;
}
Run Code Online (Sandbox Code Playgroud)